与静态祖先的事件绑定会导致触发每个点击事件

时间:2019-01-21 15:36:57

标签: javascript jquery uielement

我创建了一个小型测试应用程序,该应用程序应允许用户创建:

  • 创建列表元素
  • 删除列表元素
  • 创建列表
  • 删除列表

我了解到动态添加的元素没有绑定到事件(基于此:Event binding on dynamically created elements?),因此我尝试将事件附加到单个静态祖先上。不幸的是,这导致仅单击一个按钮就触发每个单击事件。

<head>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <title>Test Page</title>
</head>
<body>
    <div id="container">
        <div class="listContainer">
            <ol>
                <li>First list <button class="btnDeleteListItem">Delete</button></li>
                <li>First list <button class="btnDeleteListItem">Delete</button></li>
                <li>First list <button class="btnDeleteListItem">Delete</button></li>
            </ol>
            <button class="btnAddListElement">+Add List Item</button>
            <button class="btnDeleteList">Delete List</button>
        </div>
        <button class="btnAddList">+Add List</button>
    </div>
</body>
<script>
var btnDeleteListItem = $(".btnDeleteListItem");
var btnAddListElement = $(".btnAddListElement");
var btnDeleteList = $(".btnDeleteList");
var btnAddList = $(".btnAddList");
var container = $("#container");

container.on("click",btnDeleteListItem, function (e) {
    $(this).parent().remove();
    console.log("list item deleted");
});

container.on("click",btnAddListElement, function (e) {
    var html = '<li> Just added <button class="btnDeleteListItem">Delete</button></li>';
    $("ol").append(html);
    console.log("list item added");
});

container.on("click",btnDeleteList, function (e) {
    $(this).parent().remove();
    console.log("list deleted");
});

btnAddList.on("click", function(e){
    var html = '<div class="listContainer"><ol><li>Other list <button class="btnDeleteListItem">Delete</button></li><li>Other list <button class="btnDeleteListItem">Delete</button></li><li>Other list <button class="btnDeleteListItem">Delete</button></li></ol><button class="btnAddListElement">+Add List Item</button><button class="btnDeleteList">Delete List</button></div>';
    $(html).insertBefore($(this));
    console.log("list added");
});
</script>

</html>`

是否可以隔离点击事件?

1 个答案:

答案 0 :(得分:2)

问题是因为,尽管您使用的是委托事件处理程序,但是您仅在DOM加载时选择了元素。要解决此问题,请将选择器参数作为字符串传递给on()。这样,选择器将在click事件通过#container冒泡时进行匹配,而不仅仅是特定元素实例。

还请注意,您需要在append()上进行ol调用,仅调用{em> ,该调用相对于单击的.btnAddListElement按钮,否则结束将新的li添加到每个列表中。为此,请将$('ol')更改为$(this).prev('ol')

var $container = $("#container");

$container.on("click", ".btnDeleteListItem", function(e) {
  $(this).parent().remove();
  console.log("list item deleted");
});

$container.on("click", '.btnAddListElement', function(e) {
  var html = '<li> Just added <button class="btnDeleteListItem">Delete</button></li>';
  $(this).prev("ol").append(html);
  console.log("list item added");
});

$container.on("click", '.btnDeleteList', function(e) {
  $(this).parent().remove();
  console.log("list deleted");
});

$container.on('click', '.btnAddList', function(e) {
  var html = '<div class="listContainer"><ol><li>Other list <button class="btnDeleteListItem">Delete</button></li><li>Other list <button class="btnDeleteListItem">Delete</button></li><li>Other list <button class="btnDeleteListItem">Delete</button></li></ol><button class="btnAddListElement">+Add List Item</button><button class="btnDeleteList">Delete List</button></div>';
  $(html).insertBefore($(this));
  console.log("list added");
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>

<div id="container">
  <div class="listContainer">
    <ol>
      <li>First list <button class="btnDeleteListItem">Delete</button></li>
      <li>First list <button class="btnDeleteListItem">Delete</button></li>
      <li>First list <button class="btnDeleteListItem">Delete</button></li>
    </ol>
    <button class="btnAddListElement">+Add List Item</button>
    <button class="btnDeleteList">Delete List</button>
  </div>
  <button class="btnAddList">+Add List</button>
</div>