jquery尋找父元素、子項目

來源:互聯網
上載者:User

 對使用js或者jquery尋找父元素、子項目比較混淆的朋友可以參考下本文,因為是個人總結,用起來會比較方便

使用js或者jquery尋找父元素、子項目經常遇到。可是用起來總容易混淆,這裡統一總結了一下,以後用起來相信會方便好多 

 

這裡jquery向上尋找父元素 用到的方法:closest() parents() parent() 

 

向下尋找子項目 用到的方法:find() children() 

 

js用的是 children[] 屬性 

 

html代碼 

 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

<title>jquery尋找父元素子項目</title> 

 

 

</head> 

<body> 

 

 

<div class="div1" id="div1" name="mydiv"> 

<p>段落1 尋找父元素</p> 

<table id="table1"> 

 

<tbody id="tbody1"> 

<tr> 

<td id="mytd1">11closest()向上尋找最近的元素(返回零個或一個元素的 jQuery 對象)</td> 

 

</tr> 

 

<tr id="mytr2"> 

<td id="mytd2">21parent()方法</td> 

</tr> 

 

<tr> 

<td id="mytd3">31parent("選取器")方法</td> 

</tr> 

</tbody> 

 

</table> 

</div> 

 

 

<hr> 

 

<div id="div2" style="border-bottom :5px;" name="mydiv"> 

<p>段落2 尋找子項目</p> 

<table id="table2"> 

<tbody> 

<tr> 

<td id="sectd1">尋找table2的td find()方法</td> 

</tr> 

<tr id="sectr2"> 

<td id="sectd2">尋找table2的td children()方法</td> 

</tr> 

<tr> 

<td id="sectd3">js的children[]屬性來尋找</td> 

</tr> 

 

</tbody> 

 

<tbody> 

<tr> 

<td>tbody2222</td> 

</tr> 

</tbody> 

</table> 

</div> 

</body> 

</html> 

 

js代碼: 

 代碼如下:

<script type="text/javascript" src="./js/jquery-1.7.2.js"></script> 

<script> 

 

$(function(){ 

/************ 尋找父元素 *************/ 

//closest()方法 

$("#mytd1").bind("click",function(){ 

//alert($(this).html()); 

alert($(this).closest("table").attr("id")); //table1而不是table0 

//alert($(this).closest("table").html()); 

}); 

 

//parent()方法 

$("#mytd2").bind("click",function(){ 

//alert($(this).html()); //$(this).html()是21 (this).attr("id")是mytd2 

alert($(this).parent().parent().parent().attr("id")); 

//.parent()是tr 第二個.parent是tbody。即使沒有tbody標籤,找到的也是tbody 第三個.parent()是table 

 

//document.write("第一個parent的id:" + $(this).parent().attr("id") + "。 第二個parent的id是:"+$(this).parent().parent().attr("id") + "。 第三個parent的id是:"+$(this).parent().parent().parent().attr("id")); 

 

}); 

 

//parent("選取器") parents("選取器") 

$("#mytd3").bind("click",function(){ 

$("p").parent("#div1").css("background", "yellow");//這裡換成了p標籤。不知道為什麼用this找不到元素 

//alert($(this).parent("#div").attr("id"));//undefined 

alert($(this).parents("div").attr("id"));//div1 注意一個parent parents 

}); 

 

 

/************ 尋找子項目 *************/ 

//尋找table2的td元素 find() 

$("#sectd1").bind("click",function(){ 

alert($("#table2").find("td").length); 

/* $("#table2").find("td").each(function(index,element){ 

alert($(element).text()); 

}); */ 

}); 

 

//children() 

$("#sectd2").bind("click",function(){ 

var table = $("#table2"); 

alert($("#table2").children().children().children("td[id='sectd2']").html()); 

//children() 是 tbody children()是 tr children("td[id='sectd2']")是td 

}); 

 

 

// js的 children[] 

$("#sectd3").bind("click",function(){ 

var table = document.getElementById("table2"); 

alert(table.children[0].children[2].children[0].innerHTML); 

//children[0] 是 tbody children[2]是 第三行的tr children[0]是td 

}); 

 

}); 

</script> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.