jQuery 如果宽度小于.. 更改 src attr,否则,使用以前的

jQuery if width is less then.. change src attr, else, use the previous

(抱歉我的英语不好,希望你能理解) 我希望当 window width 小于 601px 时,带有 .logo-wrap 容器的图像的 src 属性变为 #;当宽度较大时,src attr 返回为 "original";代码的第一部分有效,但在我缩小 window 然后将其放大后,src 属性仍然 #...

有什么建议吗? 提前致谢

var $window = $(window);

function checkWidth() {
    var windowsize = $window.width();
    if (windowsize < 601) {
        $(".logo-wrap img").attr("src","#");
    }
    else {}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);   

看来您只是没有改回属性,

function checkWidth() {
    var windowsize = $window.width();
    if (windowsize < 601) {
        $(".logo-wrap img, .lab-ah img").attr("src","#");
    }
    else if ( windowsize >= 601) {
        $(".logo-wrap img, .lab-ah img").attr("src","original");
    }
}

希望我正确理解了你的问题

var $window = $(window);

// creates an attribute called data-content for each image and stores it's src
$(".logo-wrap img").each(function() {
  $(this).data('data-content', $(this).attr('src'));
});

function checkWidth() {
    var windowsize = $window.width();
    if (windowsize < 601) {
        $(".logo-wrap img").attr("src","#");
    }
    else {
      // changes each image's src to the link stored in data-content attribute
      $(".logo-wrap img").each(function() {
        $(this).attr("src", $(this).data('data-content'));
      });
    }
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);

编辑:添加了 JSFiddle