jQuery没有'我不认识阶级的变化

jQuery doesn't recognize a class change

本文关键字:变化 不认识 没有 jQuery      更新时间:2024-07-04

好的,我有一个编辑按钮,当我按下它时,它会变为"完成"按钮。

这一切都是由jQuery完成的。

        $(".icon-pencil").click(function() {
            var pencil = $(this);
            var row = $(this).parent('td').parent('tr');
            row.find('td').not(":nth-last-child(2)").not(":last-child").each(function() {
                $(this).html("hi");
            });
            pencil.attr('class', 'icon-ok-sign');
        });
        //  save item
        $(".icon-ok-sign").click(function() {
            alert("hey");
        });

当我按下"编辑"(".icon-pencil")按钮时,它的类变为.icon-ok-sign(我可以在chrome控制台中看到),

但当我点击它时,没有显示任何警报。

当我创建一个<span class="icon-ok-sign">press</span>并按下它时,会显示一个警报。

如何解决?

尝试使用$( document ).on( "click", ".icon-ok-sign", function() {...

因为不能为未来的元素注册点击事件,所以必须这样做:

$(document).on('click', '.icon-ok-sign', function() {
 alert('hey');
});

此方法提供了一种将委托的事件处理程序附加到页面的document元素,它简化了事件处理程序的使用当内容被动态添加到页面时。

使用以下脚本:

$(document).on('click','.icon-ok-sign',function(){
alert("hey");
});

试试这个:

$(".icon-pencil").click(function() {
            var pencil = $(this);
            var row = $(this).parent('td').parent('tr');
            row.find('td').not(":nth-last-child(2)").not(":last-child").each(function()            {
                $(this).html("hi");
            });
            pencil.removeAttr('class').addClass('icon-ok-sign');
        });
        //  save item
        $(".icon-ok-sign").click(function() {
            alert("hey");
        });