开发者

jQuery replace image source in IE

开发者 https://www.devze.com 2022-12-21 19:25 出处:网络
In the DOM 开发者_JS百科I am loading a static Google Map image. When the document is ready with jQuery sometimes I need to replace that image with a bigger version.

In the DOM 开发者_JS百科I am loading a static Google Map image. When the document is ready with jQuery sometimes I need to replace that image with a bigger version. Problem is that in IE the CPU goes whooooooo because IE is still looking to load the initial image with no success. Is there a way to cancel or clear the previous load?

EDIT: I should note that I use .html() inside a function when SWFObject is not successfully embedded..

I am not able to reproduce the problem in following example, but basically I use this:

JS:

$(document).ready(function() {
    $("img#myImage").html('<img id="myImage" src="images/002.jpg" />');
});

HTML

<img id="myImage" src="images/001.jpg" />


The .html() function replaces the HTML inside the img tag. Of course, there is no HTML in the img tag. Instead, use .attr():

$("img#myImage").attr('src', 'images/002.jpg');

Calling this immediately changes the source of the image and the browser should begin loading the new image.

0

精彩评论

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