jquery查找父元素、子元素(個(gè)人經(jīng)驗(yàn)總結(jié))
更新時(shí)間:2014年04月09日 16:53:49 作者:
對(duì)使用js或者jquery查找父元素、子元素比較混淆的朋友可以參考下本文,因?yàn)槭莻€(gè)人總結(jié),用起來會(huì)比較方便
使用js或者jquery查找父元素、子元素經(jīng)常遇到??墒怯闷饋砜?cè)菀谆煜?,這里統(tǒng)一總結(jié)了一下,以后用起來相信會(huì)方便好多
這里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()向上查找最近的元素(返回零個(gè)或一個(gè)元素的 jQuery 對(duì)象)</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 第二個(gè).parent是tbody。即使沒有tbody標(biāo)簽,找到的也是tbody 第三個(gè).parent()是table
//document.write("第一個(gè)parent的id:" + $(this).parent().attr("id") + "。 第二個(gè)parent的id是:"+$(this).parent().parent().attr("id") + "。 第三個(gè)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 注意一個(gè)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()向上查找最近的元素(返回零個(gè)或一個(gè)元素的 jQuery 對(duì)象)</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 第二個(gè).parent是tbody。即使沒有tbody標(biāo)簽,找到的也是tbody 第三個(gè).parent()是table
//document.write("第一個(gè)parent的id:" + $(this).parent().attr("id") + "。 第二個(gè)parent的id是:"+$(this).parent().parent().attr("id") + "。 第三個(gè)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 注意一個(gè)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實(shí)現(xiàn)無刷新DropDownList聯(lián)動(dòng)實(shí)現(xiàn)代碼
隨著Jquery1.4的發(fā)布,Jquery運(yùn)用越來越多了,讓我們來實(shí)現(xiàn)以前經(jīng)常用到的DropDownList無刷新聯(lián)動(dòng)。2010-03-03jQuery實(shí)現(xiàn)簡(jiǎn)潔的輪播圖效果實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)潔的輪播圖效果,結(jié)合實(shí)例形式分析了jQuery的事件響應(yīng)機(jī)制與頁面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-09-09Jquery原生態(tài)實(shí)現(xiàn)表格header頭隨滾動(dòng)條滾動(dòng)而滾動(dòng)
表頭是浮動(dòng)的,因?yàn)閮?nèi)容在同一頁面展示,當(dāng)滾動(dòng)時(shí),看不到列頭,為了改動(dòng)少只能使用jquery原生態(tài)實(shí)現(xiàn)滾動(dòng)2014-03-03jQuery實(shí)現(xiàn)的多選框多級(jí)聯(lián)動(dòng)插件
這篇文章主要介紹了jQuery實(shí)現(xiàn)的多選框聯(lián)動(dòng)插件,需要的朋友可以參考下2014-05-05jQuery簡(jiǎn)單實(shí)現(xiàn)兩級(jí)下拉菜單效果代碼
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)兩級(jí)下拉菜單效果代碼,基于jQuery遍歷簡(jiǎn)單實(shí)現(xiàn)菜單效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09jQuery實(shí)現(xiàn)獲取form表單內(nèi)容及綁定數(shù)據(jù)到form表單操作分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)獲取form表單內(nèi)容及綁定數(shù)據(jù)到form表單操作,結(jié)合jQuery封裝插件操作分析了jQuery針對(duì)form表單的serializeJson數(shù)據(jù)獲取及setForm數(shù)據(jù)綁定相關(guān)操作技巧,需要的朋友可以參考下2018-07-07