jquery查找父元素、子元素(個人經(jīng)驗總結(jié))
更新時間:2014年04月09日 16:53:49 作者:
對使用js或者jquery查找父元素、子元素比較混淆的朋友可以參考下本文,因為是個人總結(jié),用起來會比較方便
使用js或者jquery查找父元素、子元素經(jīng)常遇到??墒怯闷饋砜?cè)菀谆煜?,這里統(tǒng)一總結(jié)了一下,以后用起來相信會方便好多
這里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標(biāo)簽,找到的也是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標(biāo)簽。不知道為什么用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>
這里jquery向上查找父元素 用到的方法:closest() parents() parent()
向下查找子元素 用到的方法:find() children()
js用的是 children[] 屬性
html代碼
復(fù)制代碼 代碼如下:
<!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代碼:
復(fù)制代碼 代碼如下:
<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標(biāo)簽,找到的也是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標(biāo)簽。不知道為什么用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>
相關(guān)文章
Jquery實現(xiàn)無刷新DropDownList聯(lián)動實現(xiàn)代碼
隨著Jquery1.4的發(fā)布,Jquery運用越來越多了,讓我們來實現(xiàn)以前經(jīng)常用到的DropDownList無刷新聯(lián)動。2010-03-03Jquery原生態(tài)實現(xiàn)表格header頭隨滾動條滾動而滾動
表頭是浮動的,因為內(nèi)容在同一頁面展示,當(dāng)滾動時,看不到列頭,為了改動少只能使用jquery原生態(tài)實現(xiàn)滾動2014-03-03jQuery實現(xiàn)的多選框多級聯(lián)動插件
這篇文章主要介紹了jQuery實現(xiàn)的多選框聯(lián)動插件,需要的朋友可以參考下2014-05-05jQuery實現(xiàn)獲取form表單內(nèi)容及綁定數(shù)據(jù)到form表單操作分析
這篇文章主要介紹了jQuery實現(xiàn)獲取form表單內(nèi)容及綁定數(shù)據(jù)到form表單操作,結(jié)合jQuery封裝插件操作分析了jQuery針對form表單的serializeJson數(shù)據(jù)獲取及setForm數(shù)據(jù)綁定相關(guān)操作技巧,需要的朋友可以參考下2018-07-07