jq動態增加的button標簽click回調失效的問題,即動態增加的button標簽綁定事件$("button.class").click(function)無效


對於新增加的頁面元素,改變了頁面結構,如果是使用老辦法$("button.class").click(function)去監聽新的button標簽事件,會失效。

筆者的應用是文字的顯示和隱藏之間的切換,給出我的代碼:

解決辦法:

$(document).on("click",".showPwd",function () {
//                alert("show now:" + );
                $(this).parent().html(
                        $(this).attr("key")+'  '+
                        '<button class="btn btn-default btn-sm hidePwd" key="'+$(this).attr("key")+'"><span class="glyphicon glyphicon-eye-close"></span></button>');
});

$(document).on("click",".hidePwd",function () {
                //alert("show now");
                $(this).parent().html('<button class="btn btn-default btn-sm showPwd" key="'+$(this).attr("key")+'"><span class="glyphicon glyphicon-eye-open"></span></button>');
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM