比较'e.target'到一个jQuery对象 [英] Compare 'e.target' to a jQuery object

查看:493
本文介绍了比较'e.target'到一个jQuery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

( clickedObject === someDiv ) //returns true or false

我尝试了

( $(e.target) === $('.selector') ); //returns a false negative.

我的解决方法

( $(e.target).attr('class') === $('.selector').attr('class') ); //works as intended, not so clean though.

比较我点击DOM中对象的对象的正确方法是什么?

What is the right way to compare the object I clicked to an object in the DOM?

推荐答案

要检查 e.target 是否有此类,您可以使用 hasClass function。

To check if e.target has this class you can use the hasClass function.

if ($(e.target).hasClass("selector"))


$ b $ p

或者,如果你真的想比较对象,注意jQuery选择器返回一个集合

Or, if you really want to compare objects, note that jQuery selectors return a collection of items, so I think you'll want

if (e.target === $('.selector')[0])

这篇关于比较'e.target'到一个jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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