Why won't any version of IE process this Javas

2019-08-29 05:13发布

This is the code, changing a background image for a jQ plugin, Works fine on Chrome, FF, Safari and even my two smart phones. But not IE. Can anyone spot a problem?

<script type="text/javascript">
$(document).ready(function() {
$("#supersized img").attr({ 
          src: "images/bg2.jpg",
        });
    $("#supersized").attr($("img"));
});
</script>

3条回答
萌系小妹纸
2楼-- · 2019-08-29 05:41

This problem is not only Internet Explorer related, as I notice only Firefox would execute it without warnings. The reason that object keys suppose to be escaped properly with single or double quotes in your case

{ 'src': "images/bg2.jpg" } 

and also remove coma after image path.

查看更多
地球回转人心会变
3楼-- · 2019-08-29 05:43

IE is very strict on its object literal formation. Many times it does not like you to put a comma after the last property. So in this instance the comma after your src property will give IE fits, most notoriously IE 6 & 7

$("#supersized img").attr({ 
          src: "images/bg2.jpg"  //<-- notice no comma after property value because it's the last one.
        });
    $("#supersized").attr($("img"));
});
查看更多
对你真心纯属浪费
4楼-- · 2019-08-29 05:57

There's a trailing comma in your object literal. This always causes an error in IE browsers.

查看更多
登录 后发表回答