【问题标题】:Change class Jquery Not working [duplicate]更改类Jquery不起作用[重复]
【发布时间】:2015-12-28 16:27:44
【问题描述】:

我正在做一个自定义画廊,当您单击一个 div 时,它会更改 div 的 src,所以我这样做:

function change2dd1() {
    $("div.videoSel > img").attr("class", "joanamid");
    $("div.videoSel > img").attr('src', $('.videoSel2 > img').attr('src'));
}   

我还尝试更改 img 的类,这样当我点击它时,我会得到一个全屏版本,我会使用它:

$('.Fullscreen').css('height', $(document).outerWidth() + 'px');
//for when you click on an image
$('.joanamid').click(function () {
    var src = $('.joanafull').attr('src'); //get the source attribute of the clicked image
    $('.Fullscreen img').attr('src', src); //assign it to the tag for your fullscreen div
    $('.Fullscreen').fadeIn();

    if(imgChecker.height() > imgChecker.width()) {
        $(".Fullscreen img").css({"display":"block", "margin":"5% auto","z-index":"20","top":"0", "width":"30%",
"height":"40%"});
    }
});

问题是更改类根本不起作用,它保留了 div 首先拥有的相同类。有什么建议吗?

【问题讨论】:

标签: jquery


【解决方案1】:

使用event delegation:

$("body").on("click", '.joanamid', funciton() {
  // ...
});

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2017-03-25
    • 2017-03-07
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多