Javascript 如何将JSON URL传递给jQuery函数?
我在单选按钮前加载了相应的颜色变量:Javascript 如何将JSON URL传递给jQuery函数?,javascript,jquery,json,Javascript,Jquery,Json,我在单选按钮前加载了相应的颜色变量: <script type="text/javascript"> //<![CDATA[ images = {"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg", "Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg", "White":"http:\/\/ec
<script type="text/javascript">
//<![CDATA[
images = {"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg", "Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg", "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg"};
//]]>
</script>
下面是单选按钮的示例源
<label for="Black"
style="background: #fff url(http://ecx.images-amazon.com/images/I/11TeKFAzJsL._SL30_.jpg) center center no-repeat;"
title="Black" >
Black
</label>
<input
id="color_black"
name="color"
onclick="change_large_product_image(images['Black'])"
type="radio"
value="Black" />
黑色
解决问题的简单方法不是尝试更改src,而是清空容器并放置新的img。比如说
<div class"largeprod"><img src="...old..." /></div>
并使用jQuery将其清除并重新加载
$('.largeprod').empty().append('<img src="...new..."/>');
$('.largeprod').empty().append('');
如果你真的想巧妙地使用它,你可以将图像“预加载”到一个不显示的div中,然后克隆并放到你想要的地方。我使用一个系统,在该系统中,我将图像(和大量内容)放入隐藏的div中,将其id和它要进入的位置保存到一个数组中,然后当图像完成加载时,onload事件触发并将其移动到它应该进入的位置。参数
url
的值是多少?看起来您可能需要解码URL字符串以删除“\”字符
function change_large_product_image(url) {
jQuery('.largeprod img').attr('src', decodeURI(url));
return false;
}
另外,考虑TababdAdg的建议。 < P>这里是我的解决方案:
<script type="text/javascript">
var myImages = {
"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg"
,"Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg"
, "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg"
};
$(document).ready(function() {
for ( var i in myImages ) {
html = '<br />';
html += '<label for="'+i+'" style="background: #fff url('+myImages[i]+') center center no-repeat;" title="'+i+'" >'+i+'</label>';
html += '<input id="'+i+'" name="color" onclick="$(\'#largeImage\').attr(\'src\',\''+myImages[i]+'\');" type="radio" value="'+i+'" />';
html += '<br />';
$('body').append(html);
}
$('body').append("<img id='largeImage'>");
});
</script>
var myImages={
“黑色”:“http:\/\/ecx.images亚马逊.com\/images\/I\/31XwBA2m8pL.jpg”
,Slate:“http:\/\/ecx.images亚马逊.com\/images\/I\/31SqWkrSb6L.jpg”
“白色”:“http:\/\/ecx.images亚马逊.com\/images\/I\/31OJufgJT2L.jpg”
};
$(文档).ready(函数(){
for(myImages中的变量i){
html='
';
html+=''+i+'';
html+='';
html+='
';
$('body').append(html);
}
$('body')。追加(“”);
});
这是在做什么?您希望它做什么?单击单选按钮时更改图像
<script type="text/javascript">
var myImages = {
"Black":"http:\/\/ecx.images-amazon.com\/images\/I\/31XwBA2m8pL.jpg"
,"Slate":"http:\/\/ecx.images-amazon.com\/images\/I\/31SqWkrSb6L.jpg"
, "White":"http:\/\/ecx.images-amazon.com\/images\/I\/31OJufgJT2L.jpg"
};
$(document).ready(function() {
for ( var i in myImages ) {
html = '<br />';
html += '<label for="'+i+'" style="background: #fff url('+myImages[i]+') center center no-repeat;" title="'+i+'" >'+i+'</label>';
html += '<input id="'+i+'" name="color" onclick="$(\'#largeImage\').attr(\'src\',\''+myImages[i]+'\');" type="radio" value="'+i+'" />';
html += '<br />';
$('body').append(html);
}
$('body').append("<img id='largeImage'>");
});
</script>