开发者

Why won't any version of IE process this Javascript?

开发者_开发技巧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>


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"));
});


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜