Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery在创建元素并将事件设置为类时复制事件_Javascript_Jquery_Events - Fatal编程技术网

Javascript JQuery在创建元素并将事件设置为类时复制事件

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(示例); });

当我使用click event添加一个项目并且存在另一个具有相同类的元素时,该事件被复制

$(“.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(
示例); });