在jQuery中,如果需要,如何将事件传递给父锚? [英] In jQuery, how do i pass an event to a parent anchor if needed?

查看:95
本文介绍了在jQuery中,如果需要,如何将事件传递给父锚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery在clojurescript中,但是答案对于我认为的cljs和js应该是相同的.

I am in clojurescript using jQuery but the answer should be the same for cljs and js I think.

我有一个辅助函数,该函数创建一个创建锚元素,然后在其中放置一个图标元素.我的锚为按钮的类型提供了一个独特的类.

I have a helper function which creates creates an anchor element then puts an icon element inside of it. My anchor has a unique class for the type of button.

当我做类似的事情

$('.my-btn-type').click(function(e){console.log(e.target)});

$('.my-btn-type').click(function(e) {console.log(e.target)});

  • 如果图标被单击(即使处理程序位于父级上),也会打印
  • 仅单击锚点时打印
  • 我经常遇到一个问题,即由锚点包裹的图标是事件目标,而不是附加了处理程序的锚点.我在我的处理程序函数中需要的锚元素中使用数据属性.这迫使我手动检查event.target是图标还是锚点,然后手动使事件冒泡或前进.

    I constantly have an issue where the icon which is wrapped by the anchor is the event target and not the anchor with the handler attached to it. I am using data attributes in my anchor element which are needed in my handler function. This forces me to manually check if the event.target is the icon or the anchor and then manually bubble up the event or move forward.

    这似乎有点骇人听闻,很痛苦.必须有一种简单的方法来附加处理程序(使用jQuery,js或clojurescript),该处理程序仅调用处理程序绑定到的元素.我该怎么做?

    This seems hackish and is a pain. There must be a simple way to attach a handler, in jQuery, js, or clojurescript, which would only call the element that the handler is bound to. How do I do this?

    使用"this"是我所需要的,但是在clojurescript中,需要this-as宏.该线程有一个示例在此处输入链接描述

    Using "this" was what I needed but in clojurescript the this-as macro is needed. This thread has an exampleenter link description here

    推荐答案

    如果我理解正确,似乎您正在为锚点和图标挂钩click事件.

    If I understood it right it seems like you are hooking up the click event for both the anchor and icon.

    或者锚点和图标具有相同的类名.

    Or the anchor and the icon have the same class name.

    例如:

    <a href="#" class="my-btn-type" >
        <img src="../img/icon.png" class="my-btn-type" />
    </a>
    

    这篇关于在jQuery中,如果需要,如何将事件传递给父锚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆