jquery 獲取元素(父節點,子節點,兄弟節點)

費話很少說,直接上代碼
jquery 獲取元素jquery

 1 $("#test1").parent(); // 父節點
 2 $("#test1").parents(); // 所有父節點
 3 $("#test1").parents(".mui-content");//返回class爲mui-content的父節點
 4 $("#test1").children(); // 所有子節點
 5 $("#test1").children("#test1"); //返回id爲test1的子節點
 6 $("#test1").contents(); // 返回id爲test裏面的全部內容,包括節點和文本
 7 $("#test1").contents("#test1"); //返回id爲test裏面的id爲#test1的節點和文本
 8 $("#test1").prev();  // 上一個兄弟節點
 9 $("#test1").prevAll(); // 以前全部兄弟節點
10 $("#test1").next(); // 下一個兄弟節點
11 $("#test1").nextAll(); // 以後全部兄弟節點
12 $("#test1").siblings(); // 全部兄弟節點
13 $("#test1").siblings("#test2"); //返回id爲test2的兄弟節點
14 $("#test").find("#test1"); 選中id爲test後代中 id爲test1的節點
View Code

注意上面的方法 返回的是jquery 集合 須要繼續用jq的方法操做或取值
若是想要轉爲dom 直接操做的只須要 取它的下標便可
如: $("#test1").parent()[0] 返回的就是dom節點dom

相關文章
相關標籤/搜索