对于新增加的页面元素,改变了页面结构,如果是使用老办法$("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>');
});