事件传播和多个事件处理程序

发布于 2024-11-25 10:50:55 字数 1142 浏览 0 评论 0 原文

好吧,我很困惑。我在 document 上有一个全局点击事件处理程序。在页面上我有一些链接。每个链接都由相同的单击事件处理程序处理,该处理程序除其他外,还可以防止事件冒泡到文档级别并防止链接执行。在这些链接中,有一个特定的点击处理程序,应该执行其任务,然后将事件传递给链接的通用点击事件。但事实并非如此。

document.onclick = function()
  {
   document.body.innerHTML += "You clicked me!"; 
};

  document.getElementsByTagName("a")[0].onclick = function(e) {
   this.innerHTML += " Click it!";
    e.stopPropagation();
    //This return false appears only to
    //prevent the link from its default action
  return false; 
  };
document.getElementById("link").onclick = function(e) {
  this.innerHTML += " Go ahead, ";
  //But this return false appears to stop
  //the propagation up to the previous event
  //I would think that removing the link below
  //would cause the event to propagate to the event
  //above which would then stop the propagation and
  //prevent the default, but apparently this is 
  //not the case; removing the line below causes
  //the link to load Google like normal
  return false;
};

如何让较低的事件触发并触发较高的事件,然后取消该事件?

明白我的意思此处

Okay, I'm confused. I've got a global click event handler on document. On the page I have a few links. Each link is handled by the same click event handler that, among other things, prevents the event from bubbling to the document level and prevents the link from executing. Of these links, one has a specific click handler that is supposed to do its thing and then pass off the event to the generic click event for the links. But it's not.

document.onclick = function()
  {
   document.body.innerHTML += "You clicked me!"; 
};

  document.getElementsByTagName("a")[0].onclick = function(e) {
   this.innerHTML += " Click it!";
    e.stopPropagation();
    //This return false appears only to
    //prevent the link from its default action
  return false; 
  };
document.getElementById("link").onclick = function(e) {
  this.innerHTML += " Go ahead, ";
  //But this return false appears to stop
  //the propagation up to the previous event
  //I would think that removing the link below
  //would cause the event to propagate to the event
  //above which would then stop the propagation and
  //prevent the default, but apparently this is 
  //not the case; removing the line below causes
  //the link to load Google like normal
  return false;
};

How do I get the lower event to fire and up to the upper event which then cancels the event?

See what I mean here

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

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

发布评论

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

评论(1

你是我的挚爱i 2024-12-02 10:50:55

哈,呃。使用 element.on 只是在 DOM 中为元素设置属性,这意味着每个事件只能有一个处理程序。相反,我需要使用 addEventListener 并结合正确使用 event.preventDefault()event.stopPropagation()

在我的第一次尝试中,我把我想成为第一个处理程序的东西放在第二位,但这实际上意味着它会覆盖第一个处理程序。在这种情况下,我需要先放置我想要的处理程序,因为该处理程序附加到该事件。

我修改后的代码应该是:

document.onclick = function()
  {
   document.body.innerHTML += "You clicked me!"; 
};
document.getElementById("link").addEventListener("click",function() {
  this.innerHTML += " Go ahead, ";
});
  document.getElementsByTagName("a")[0].addEventListener("click",function(e) {
   this.innerHTML += " Click it!"; 
    e.stopPropagation();
    e.preventDefault();
   });

http://jsbin.com/ecozep/8/edit

Ha, duh. using element.on<event> is simply setting the property in the DOM for the element, which means each event can only have one handler. Instead, I need to use addEventListener combined with a proper use of event.preventDefault() and event.stopPropagation()

In my first attempts, I was putting what I wanted to be the first handler second, but that really meant it was overriding the first. In this case, I need to put the handler I want first first, since the handler is being attached to the event.

My revised code should be:

document.onclick = function()
  {
   document.body.innerHTML += "You clicked me!"; 
};
document.getElementById("link").addEventListener("click",function() {
  this.innerHTML += " Go ahead, ";
});
  document.getElementsByTagName("a")[0].addEventListener("click",function(e) {
   this.innerHTML += " Click it!"; 
    e.stopPropagation();
    e.preventDefault();
   });

http://jsbin.com/ecozep/8/edit

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