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

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

jQuery test if element1 is descendant of element2

Does anyone know a good way to test if one element, stored in a var, is the descendant of another, also stored in a var?

I don't need element1.isChildOf('selector'), that's easy.
I need element1.isChildOf(element2)

element2.find(element1).size() > 0 Does not seem to work.

I don't want to have to write a plugin the uses .each to test each child if I can avoid it.

question from:https://stackoverflow.com/questions/2398827/jquery-test-if-element1-is-descendant-of-element2

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

1 Answer

0 votes
by (71.8m points)

If you're using 1.4, and are looking for a descendant rather than a child as your find() example implies, there's a has() method:

element2.has(element1).length > 0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...