开发者

Reload all images on page

I'm trying to get all the images in the page to reload including backgound-image: rules

I have some semi working code but I want to know if there is an easier way of doing this.

function cacheBuster(url) {
    return url.replace(/\?cacheBuster=\d*/, "") + "?cacheBuster=" + new Date().getTime().toString();
}

$("img").each(function() {
    this.src = cacheBuster(this.src);
});

$("*").each(function() {
    var bg_img = $(this).css("background-image");
    if (bg_img !== "none") {
        var url = /url\((.*)\)/i.exec(bg_img);
        if (url) {
            $(this).css("background-image", "url(" + cacheBuster(u开发者_JAVA技巧rl[1]) + ")");
        }
    }
}); 


Looks fine but you are missing inputs with type=image, that is images that act as a submit button. You can include them by adding following code

$("img, input[type=image]").each(function() {
    this.src = cacheBuster(this.src);
});

Also you can change the code where you loop through all elements, just to include visible ones, if it is acceptable in your case.

$("*:visible")

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜