javascript - DIVs点击事件错误触发其他DIVs点击事件

标签 javascript jquery events

<分区>

我正在尝试绑定(bind) div 元素的点击事件以显示它们各自的模态。

我传递了每个元素的 ID,函数报告它绑定(bind)了正确的 ID。

当我实际点击一个 div 时,点击事件总是针对最终 ID 触发。

我检查了 DIV 的 HTML,它们都有正确的 ID。

<div id="e800" data-target="#event800" class="event" data-eid="800">
  <p>Click Me</p>
  <div id="event800" style="display: none;" role="dialog" tabindex="1" class="modal">
     <p>I am another DIV</p>
  </div>
</div>

<div id="e801" data-target="#event801" class="event" data-eid="801">
  <p>Click Me</p>
  <div id="event801" style="display: none;" role="dialog" tabindex="1" class="modal">
     <p>I am another DIV</p>
  </div>
</div>

<div id="e802" data-target="#event802" class="event" data-eid="802">
  <p>Click Me</p>
  <div id="event802" style="display: none;" role="dialog" tabindex="1" class="modal">
     <p>I am another DIV</p>
  </div>
</div>


function BindEvents(ids) {

  for(var i = 0, l = ids.length; i < l; i++) {
    /* ID of element - ex: #e800 */
    var e = "#e" + ids[i];

    /* ID of element I want to show - ex: #event800 */
    var id = $(e).data('target');

    /*
       This console.log outputs the correct e and id. Ex:
       "Binding click of #e800 to show #event800"
    */
    console.log("Binding click of " + e + " to show " + id);

    $(e).on('click', function(ev) {
      /*
        This console.log outputs the e and id of the very last div.
        I click #e800 and the output for this is :
        "Click event of #e802 now showing #event802"
      */
      console.log("Click event of " + e + " now showing " + id);
      $(id).show();
      $('#backdrop').show();
    });

    /*
      $(id) element is a child of the $(e) element.
      If we do not call ev.stopPropagation() then
      the click will also be triggered in $(e).
      This would cause the modal to reshow directly
      after being hidden.
      Which means once clicked, it would never hide.
    */

    $(id).on('click', function(ev) {
      ev.stopPropagation();
      $(id).hide();
      $('#backdrop').hide();
    });
  }
}

我的问题的解决方案在重复问题中。

虽然我已将我的代码更改为已接受的答案并删除了循环。

function BindEvents(ids) {

  $(document).on('click', '.event-bind', function() {
    var id = $(this).data('target');

    $(id).show();
    $('#backdrop').show();

    $(id).one('click', function(ev) {
      ev.stopPropagation();
      $(id).hide();
      $('#backdrop').hide();
    });

  });
}

最佳答案

我建议在所有这些类上放置一个(即class="showModalOnClick")“编辑”——可能会改变它以适应更好地满足您的需求。

然后,使用单个绑定(bind)事件:

    // bind to document instead of directly to the class in case you have 
    // some of the items to click being added after the document is loaded.
    $(document).on('click','.edit',function(){
        var $this = $(this);
        var e = $this.attr('id');
        var id = $this.data('target');

        // This console.log outputs the e and id of the very last div.
        // I click #e800 and the output for this is :
        // "Click event of #e805 now showing #event805"
        console.log("Click event of " + e + " now showing " + id);
        $(id).show();
        $('#backdrop').show();

        // $(id) element is a child of the $(e) element.
        // If we do not call ev.stopPropagation() then
        // the click will also be triggered in $(e).
        // This would cause the modal to reshow directly
        // after being hidden.
        // Which means once clicked, it would never hide.
        // .one will run this event once and unbind after
        $(id).one('click', function(ev) {
            ev.stopPropagation();
            $(id).hide();
            $('#backdrop').hide();
        });
    });

关于javascript - DIVs点击事件错误触发其他DIVs点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884894/

相关文章:

javascript - 检测 DOM 元素中的属性变化

javascript - 当您使用 JavaScript 单击按钮时,使我的页面变黑

javascript - 粘贴文本时基于字数限制文本输入的 jQuery 函数不起作用

javascript - Rails 没有处理我的 jQuery .click 事件(甚至无法弹出警报。)

JavaScript 事件目标

android - 注入(inject) screen_on 事件以使传感器在屏幕关闭时工作

javascript - 我的Promise捕获异常,我不知道如何打印错误消息

javascript - 主干单击事件属性

javascript - 如何在 Javascript 中检查实例的类?

jquery - 无法让 fancybox 1.3.4 工作