簡體   English   中英

jQuery更改img src且不顯示舊

[英]jQuery Change img src and do not display old

我正在使用jQuery替換圖像src路徑的一部分。 它可以完美工作,但在顯示新圖像源之前仍會渲染舊圖像。 我想知道是否可以使用.hide()和.show()來防止渲染舊圖像。

$(document).ready(function() {
    $(".gallery img").each(function() {
        $(this).attr("src", function(a, b) {
            return b.replace("s72-c", "s190-c")
        })
    })
});

圖片網址結構:

舊圖片網址: http//www.example.com/s72-c/sample.jpg

新圖片網址: http : //www.example.com/s190-c/sample.jpg

迭代前的hide()和迭代中的show()

$(document).ready(function() {
    $(".gallery img").hide().each(function() {
        $(this).attr("src", function(a, b) {
            return b.replace("s72-c", "s190-c")
        });
        $(this).show();
    })
});
$(document).ready(function() {
    $("img").each(function() {
        $(this).attr("src",'');
        $(this).attr("src", function(a, b) {
            return b.replace("https://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg", "https://upload.wikimedia.org/wikipedia/commons/7/79/Apple_seeds_-_variety_Gala_%28aka%29.jpg")
        })
    })
});

檢查此代碼

使用removeAttr()

$(document).ready(function() {
    $(".gallery img").each(function() {
        var src = this.src;
        $(this).removeAttr('src').attr("src", src.replace("s72-c", "s190-c"));
    })
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM