有什么方法可以在 jQuery 中委托事件吗?

发布于 2024-11-26 17:49:46 字数 209 浏览 1 评论 0原文

我想委托事件one 用于点击。有谁知道是否可以做到?

I would like to delegate the event one for the click. Does anyone know if it is possible to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荆棘i 2024-12-03 17:49:46

我假设您希望事件仅在每个匹配元素上触发一次,而不是在第一次单击时完全取消绑定。

我会像这样实现它:

$('#container').delegate('.children', 'click', function() {
  if($(this).data('clicked')) {
      return;
  }

  // ... your code here ...


  $(this).data('clicked', true);

});

每个元素只会触发一次。从技术上讲,它每次都会触发,但在第一次单击时会被标记,因此代码不会再次执行。

使用委托模拟 .one() 处理程序的固有问题是,使用 .one() 选择器中匹配的每个元素都绑定其自己的事件处理程序。因此,当第一次触发它时,它会从该元素中解除绑定/删除处理程序。您不能使用 .delegate() 执行此操作,因为只有一个处理程序用于所有匹配的元素。

虽然上面的代码完美地模拟了它,但它仍然有点hackish,因为它实际上并没有执行与 .one() 相同的操作(解除绑定事件处理程序)。

I'm going to assume that you want the event to fire only once PER matched element rather than unbind entirely on the first click.

I'd implement it like so:

$('#container').delegate('.children', 'click', function() {
  if($(this).data('clicked')) {
      return;
  }

  // ... your code here ...


  $(this).data('clicked', true);

});

This will fire only once per element. Technically, it fires everytime but is flagged the first time it is clicked so the code will not execute again.

The inherent problem of simulating a .one() handler w/ delegate is that using .one() each element that was matched in the selector is bound its own event handler. So when it is fired for the first time it unbinds/removes the handler from that element. You can't do that with .delegate() because only a SINGLE handler is being used for ALL the matched elements.

While the code above simulates it perfectly, it is still somewhat hackish because it doesn't literally do the same thing that .one() does (unbinding an event handler).

太阳男子 2024-12-03 17:49:46

由于这篇文章已有几年历史,我只是想为更多当代读者提供一个完整的更新示例(2015)。其逻辑与此处的其他答案没有什么不同,但 jQuery 的方法自 2011 年以来一直在发展。具体来说:

从 jQuery 1.7 开始,.delegate() 已被 .on() 方法取代。
jQuery delegate()

// define output element
var $output = $('div#output');

// attach delegated click handler
$(document).on('click', 'button', function() {
  
  // define clicked element
  var $this=$(this);
  
  // if this element has already been clicked, abort
  if ($this.data('clicked')) {
    return false;
  }
   
  // perform click actions
  $output.append("clicked " + $this.html() + "<br />");
 
  // mark this element as clicked
  $this.data('clicked',true);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>One</button>
<button>Two</button>
<button>Three</button>

<div id="output"></div>

Since this post is a few years old, I just wanted to provide a complete updated example for more contemporary readers (2015). The logic is no different from the other answers here, but jQuery's methods have evolved since 2011. Specifically:

As of jQuery 1.7, .delegate() has been superseded by the .on() method.
jQuery delegate()

// define output element
var $output = $('div#output');

// attach delegated click handler
$(document).on('click', 'button', function() {
  
  // define clicked element
  var $this=$(this);
  
  // if this element has already been clicked, abort
  if ($this.data('clicked')) {
    return false;
  }
   
  // perform click actions
  $output.append("clicked " + $this.html() + "<br />");
 
  // mark this element as clicked
  $this.data('clicked',true);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>One</button>
<button>Two</button>
<button>Three</button>

<div id="output"></div>

葮薆情 2024-12-03 17:49:46

我确信有一种简洁的方法可以做到这一点,但一种简单的方法是这样的:

<script>
  $(document).ready(function(){
    $("#container").delegate('.clickers', 'click', function(){
      if($(this).data("clicked")==null){
        $(this).data("clicked", "true");
        $("#container").append($(this).html());
      }
    });
  });
</script>
<div class="clickers" clicked="false"></div>
<div class="clickers" clicked="false"></div>

编辑:感谢下面的评论,我决定使用数据,现在这不'不要为了 w3c 标准而把 DOM 搞砸。

I'm sure there is a neat way of doing it, but a simple way to do it would be something like this:

<script>
  $(document).ready(function(){
    $("#container").delegate('.clickers', 'click', function(){
      if($(this).data("clicked")==null){
        $(this).data("clicked", "true");
        $("#container").append($(this).html());
      }
    });
  });
</script>
<div class="clickers" clicked="false"></div>
<div class="clickers" clicked="false"></div>

EDIT: Thanks to the comments below I decided to use data, now this doesn't screw the DOM all up for w3c standards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文