在动态增加的内容中包含点击按钮,但是通过jquery的click无法触发
$('#addone').click(function () { var appendStr= " \n" + " <p>\n" + " </p><p>\n" + " <button>\n" + " <i></i>\n" + " </button>\n" + " </p>\n" + " \n" + " "; $("#firstone").append(appendStr) }); $("#firstone").on("click",".deleteone",function () { alert(1234) $(this).closest(".layui-row").remove(); })
比如通过id为 addone
的按钮向id
为 firstone
的p中动态加入一个包含class
为deleteone
的按钮的p,点击这个按钮可以触发事件,但是发现
$('.deleteone ').click(function () { })
没任何反应
现在需要使用到jquery的on事件,通过把click事件向元素中的指定内容附加
$("#firstone").on("click",".deleteone",function () { alert(1234) $(this).closest(".layui-row").remove(); })
推荐教程:《JS教程》
以上就是追加的html代码使用jquery的click事件无效的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号