簡體   English   中英

jQuery無法識別類更改

[英]jQuery doesn't recognize a class change

好的,我有一個編輯按鈕,當我按下它時,它變為“完成”按鈕。

一切都由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");
        });

暫無
暫無

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

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