How to reach the uncle using jquery

2019-02-16 16:48发布

<div id="grandfather">
  <div id="uncle"></div>
  <div id="father>
    <div id="me"></div>
  </div>
</div>

I am at $("#me") , and I want to select my uncle, using stuff like :

 $("#me").find("#uncle")
 $("#me").next("#uncle")
 $("#me").prev("#uncle")

How ?

3条回答
【Aperson】
2楼-- · 2019-02-16 16:54
$(this).parent().prev().append("This is uncle");
查看更多
Luminary・发光体
3楼-- · 2019-02-16 17:10

You could use $.parent and $.prev assuming your uncle is always above your father:

$(this).parent().prev(); // where 'this' is #me

You could also go all the way up to your grandfather, and find uncles from there:

$(this).parents("#grandfather").find(".uncles");

Or you could search your father's siblings:

$(this).parent().siblings("#uncle");

I would encourage you to read the Traversing portion of the jQuery API for various other methods.

查看更多
We Are One
4楼-- · 2019-02-16 17:14
var uncle = $('#me').parent().prev();
查看更多
登录 后发表回答