How to reach the uncle using jquery

笑着哭i 提交于 2019-12-09 03:05:00

问题


<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 ?


回答1:


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.




回答2:


var uncle = $('#me').parent().prev();



回答3:


$(this).parent().prev().append("This is uncle");


来源:https://stackoverflow.com/questions/10459418/how-to-reach-the-uncle-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!