Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What I want to do:

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

What I tried

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

My workaround

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

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

question from:https://stackoverflow.com/questions/8606269/compare-e-target-to-a-jquery-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
83 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...