简体   繁体   English

从我的自定义点击处理程序调用默认事件处理程序?

[英]Calling the default event handler from my custom click handler?

I am using jQuery to implement event delegation on my page.我正在使用 jQuery 在我的页面上实现事件委托。 I define a click handler for the top level element, but only want to do something "custom" if the click fell on an element of certain class:我为顶级元素定义了一个点击处理程序,但如果点击落在某个类的元素上,我只想做一些“自定义”:

$("#myDivWithManyLinks").click(function(e){
   var target = $(e.target);
   if (target.hasClass('myClass123')
   {
       // do my "custom thing"
       return false;
   }
   else
   {
       // XXX let the click be handled by the click handler that would otherwise get it
   }

How can I do "XXX"?我该怎么做“XXX”?

write a handler function and then reference it in else:编写一个处理函数,然后在 else 中引用它:

   $("#myDivWithManyLinks").click(function(e){
    var target = $(e.target);
    if (target.hasClass('myClass123')
    {
       // do my "custom thing"
       return false;
    }
    else
    {
       handler(e);
    }
   });
   function handler(e){
      // what you want all links that don't have your special class to do
   }

what about preventDefault() : preventDefault()怎么样:

$("#myDivWithManyLinks").click(function(e){
    var target = $(e.target);
    if (target.hasClass('myClass123')
    {
       e.preventDefault();
       //do custom stuff
    }

   });

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

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