對於新增加的頁面元素,改變了頁面結構,如果是使用老辦法$("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>'); });