jquery, IE 8 Invalid argument error
This code works fine in FF, Chrome & Safari, but IE 8 is th开发者_运维百科rowing several Invalid Argument Errors when I run it:
<script type="text/javascript">
$(window).load(function() {
$('div .box').each(function() {
$(this).width($(this).find('img').width());
});
});
</script>
You can see it in context here: My html/css skills are more or less spotless but I'm practically clueless with jquery/javascript. I have searched but cant get a handle on what the error is and how to fix it. Any help greatly appreciated.
Thanks.
The code "$(this).find('img')" is not finding an image in all cases. Try:
$(window).load(function() {
$('div .box').each(function() {
var imageJQObject = $(this).find('img');
if(imageJQObject.length > 0) {
$(this).width(imageJQObject.width());
}
});
});
is there any other information on the exception, like which line it is? I'm getting the feeling that the width of the image is not being found so you are setting the width of this
with null. try and substitute the img width with something like 100 and see if it still throws.
Looking at the markup on your page, you should have
$(window).load(function() {
$('div.box').each(function() {
$(this).width($(this).find('img').width());
});
});
Notice the missing space between div
and .box
. If you write them separately, that means you are looking for all the elements with class box
which are children of div
while without the space it finds all the div
with a class box
精彩评论