简体   繁体   English

当点击任何“a”标签时,如何使点击处理程序处理,无论它是什么包裹?

[英]How to make click handler handle when ANY “a” tag is clicked, regardless of what it's wrapped around?

Let's say I have this in my HTML page: 假设我在HTML页面中有这个:

<a href="/foo">bar</a>

<a href="/">
    <div></div> 
</a>

And I want to write a handler that handles when ANY "a" tag is clicked (with jQuery): 我想编写一个处理程序来处理何时单击任何“a”标记(使用jQuery):

$(document).click((e) => {
  const element = e.target;
  if (element && element.nodeName === 'A') {
    // Do something
    e.preventDefault();
  }
});

The above code only works for the top "a" tag, but not the bottom one. 上面的代码仅适用于顶部的“a”标签,但不适用于底部的标签。 For the top "a" tag, element.nodeName equals A . 对于顶部的“a”标记, element.nodeName等于A For the bottom "a" tag, element.nodeName equals DIV . 对于底部的“a”标记, element.nodeName等于DIV

How do I write a click handler that handles whenever ANY "a" tag is clicked, regardless of what it is wrapped around? 我如何编写一个单击处理程序来处理任何“a”标记被点击时,无论它被包裹什么?

Capture delegated events in any parent element. 捕获任何父元素中的委托事件 Document will get all events if you don't call preventDefault before it arrives during the bubbling phase. 如果您在冒泡阶段到达之前未调用preventDefault ,则文档将获取所有事件。 Read about Event phases, it really pays off! 阅读有关活动阶段的信息,它真的得到了回报!

$(document).on("click", "a", function(event) {

    // You have clicked an anchor
    window.console.log ("You have clicked this anchor:", this," but you clicked maybe inside a div or a something inside the <a>", event.target);

});

https://jsfiddle.net/7ttm4y8k/4/ https://jsfiddle.net/7ttm4y8k/4/

Note: As you are delegating the events to the document, this will work even for elements that were added after the handler's been set. 注意:当您将事件委托给文档时,即使对于在设置处理程序之后添加的元素,这也将起作用。 This way you can setup your delegated anchor handler then maybe at a later time add anchors from an Ajax query without the need of setting handlers to every created anchor. 这样您就可以设置委托的锚处理程序,然后可以在以后添加来自Ajax查询的锚点,而无需为每个创建的锚点设置处理程序。

https://jsfiddle.net/82fpvwrL/ https://jsfiddle.net/82fpvwrL/

  $("a").click(function(){ alert('WOO HOO! I was clicked.'); }); 

将它应用于每个锚点。

document.getElementsByTagName('a').map(tag => tag.onClick = yourHandler);

Use something like this: 使用这样的东西:

$(document).on("click","a", (e) => {
   e.preventDefault();
   const element = e.target;
   // Do something
});

This is how I do it (w/o jQuery): 这是我的方式(没有jQuery):

document.body.addEventListener('click', function(ev) {
    var targetElement = ev.target;
    while(targetElement !== null && targetElement.nodeName.toUpperCase() !== "A") {
        targetElement = targetElement.parentElement;
    }

    // Proceed only if an A element was found in the ev.target's ancestry
    if (targetElement !== null) {
        ev.preventDefault();
        // Your code here
    }
}

This will catch the event at the body tag and is oblivious to a tags being added/removed in the document. 这将赶在活动body标记,是无视a标签添加/文件中删除。 It will work as long as the click event's propagation was not stopped by any elements in the DOM tree from the element to the body . 只要click事件的传播没有被DOM树中从元素到body的任何元素停止,它就会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM