简体   繁体   English

jQuery侦听器,用于单击类,但单击其中的图标时不起作用

[英]Jquery listener for clicking a class, but not working when clicking the icon inside it

First of all, I couldn't find this question over here, so I don't know if it has been answered yet. 首先,我在这里找不到这个问题,所以我不知道它是否已经回答。

I have a listener for all the clicks in my site, and then calls a function that checks if the target has the class "wave". 我有一个用于站点中所有单击的侦听器,然后调用一个函数来检查目标是否具有“ wave”类。 If so, it displays a wave effect. 如果是这样,它将显示波浪效果。

I have tiles with this class, and it works fine, except they have large icons, and if you click them, it does not recognise it as a target with the class. 我有此类的磁贴,并且工作正常,除了它们具有大图标,并且如果单击它们,则不会将其识别为该类的目标。

I tried to put all the tiles inside a div with the class, but somehow it does not recognise it as a target with this class either (I'm assuming it recognize as clicking the target inside them). 我试图将所有磁贴都放在该类的div内,但无论如何也无法将其识别为该类的目标(我假设它可以识别为单击其中的目标)。

I tried to put the 'true' at the end of the listener, in case the bubbling direction could help me, but it didn't. 我尝试将“ true”放在听众的末尾,以防冒泡的方向对我有帮助,但事实并非如此。

Any idea? 任何想法? thanks in advance and sorry for my ignorance. 在此先感谢您,感谢您的无知。

jsfiddle jsfiddle

    <div class="tile-container">
  <div class="tile efecteona">
   <h3 class="titol-tile">Gràfics</h3>
   <i class="fa fa-bar-chart tile-icon "></i>
  </div>
</div>

"efecteona" would be "wave" class https://jsfiddle.net/qtLvef8o/2/ “效果”将是“波动”类https://jsfiddle.net/qtLvef8o/2/

As the other answers note, the target returns the actual clicked element which in this case is the icon and not the div with the class. 作为其他答案, target返回实际单击的元素,在这种情况下,该元素是图标,而不是类的div。

Since you use jQuery though, why not use its delegate syntax which allows for this ? 由于您虽然使用jQuery,但为什么不使用允许这样做的委托语法呢?

$(document).on('click', '.efecteona, button', addOnaEffect);

and set target = this inside your hander. 并在您的处理程序中设置target = this

updated demo at https://jsfiddle.net/qtLvef8o/4/ 更新的演示位于https://jsfiddle.net/qtLvef8o/4/

When you are asking about the e.target it gets the i element, and the i element doesn't have the class you need and neither had the tagname button. 当您询问e.target时,它将获得i元素,而i元素没有您需要的类,也没有标记名按钮。

Add this console.log(target); 添加此console.log(target); next to your line var target = e.target; 在您的行旁边var target = e.target; and you will see what I'm saying. 你会明白我在说什么。

When you click an element the event bubbles all the way up to the document, while the target is always the element which initialized it. 当您单击一个元素时,事件会一直冒泡直至文档,而目标始终是对其进行初始化的元素。 In your case, it seems that your code assumes the target is a tile element, while in fact it can also be the icon element. 在您的情况下,您的代码似乎假定target是tile元素,而实际上它也可以是icon元素。

Since you tagged on the question that you are using jQuery, there's a simple solution. 由于您在问题上标记了您正在使用jQuery,因此有一个简单的解决方案。 Use jQuery closest method. 使用jQuery 最接近的方法。

var target = $(e.target).closest('.efecteona, button').get(0);

Code example 代码示例

The target is wrong is this case. 这种情况下target是错误的。

I tried to explain by resetting the target when it's the parent that has the class (a bit dirty, can be optimised / better written but it's just to explicitly show the target issue) 我试图通过重置具有父类的目标来进行解释(有点脏,可以进行优化/编写得更好,但这只是为了明确显示目标问题)

https://jsfiddle.net/OxyDesign/wbxvqt2b/1/ https://jsfiddle.net/OxyDesign/wbxvqt2b/1/

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

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