Javascript JQuery在创建元素并将事件设置为类时复制事件
当我使用click event添加一个项目并且存在另一个具有相同类的元素时,该事件被复制Javascript JQuery在创建元素并将事件设置为类时复制事件,javascript,jquery,events,Javascript,Jquery,Events,当我使用click event添加一个项目并且存在另一个具有相同类的元素时,该事件被复制 $(“.add first”)。在(“单击”,函数()上){ var firstContainer='addsecond'; $(this).closest(“.first container”).append(firstContainer); $(“.add second”)。在(“单击”,函数(){ $(this).closest(.second container”).append(示例); });
$(“.add first”)。在(“单击”,函数()上){
var firstContainer='addsecond';
$(this).closest(“.first container”).append(firstContainer);
$(“.add second”)。在(“单击”,函数(){
$(this).closest(.second container”).append(
示例);
});
});
$(“.add second”)。在(“单击”,函数(){
$(this).closest(.second container”).append(
示例);
});代码>
先加
加第二
每次创建新按钮时,尝试实现而不是绑定事件
$(".add-first").on("click", function() {
var firstContainer = '<div class="second-container"><button class="add-second">add second</button></div>';
$(this).closest(".first-container").append(firstContainer);
});
$(".first-container").on("click", ".add-second", function() {
$(this).closest(".second-container").append("<br>example");
});
$(“.add first”)。在(“单击”,函数()上){
var firstContainer='addsecond';
$(this).closest(“.first container”).append(firstContainer);
});
$(“.first container”)。在(“单击“,”.add second“,函数()上){
$(this).closest(.second container”).append(
示例);
});