国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jquery查找父元素、子元素(個人經(jīng)驗總結(jié))

2024-05-06 16:03:53
字體:
供稿:網(wǎng)友
對使用js或者jquery查找父元素、子元素比較混淆的朋友可以參考下本文,因為是個人總結(jié),用起來會比較方便

使用js或者jquery查找父元素、子元素經(jīng)常遇到。可是用起來總?cè)菀谆煜@里統(tǒng)一總結(jié)了一下,以后用起來相信會方便好多

這里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>
<p>段落1 查找父元素</p>
<table>

<tbody>
<tr>
<td>11closest()向上查找最近的元素(返回零個或一個元素的 jQuery 對象)</td>

</tr>

<tr>
<td>21parent()方法</td>
</tr>

<tr>
<td>31parent("選擇器")方法</td>
</tr>
</tbody>

</table>
</div>


<hr>

<div>
<p>段落2 查找子元素</p>
<table>
<tbody>
<tr>
<td>查找table2的td find()方法</td>
</tr>
<tr>
<td>查找table2的td children()方法</td>
</tr>
<tr>
<td>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>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 元谋县| 卫辉市| 兴和县| 永济市| 浦县| 潍坊市| 汝城县| 唐山市| 高雄市| 新野县| 岐山县| 平遥县| 中阳县| 曲周县| 岚皋县| 体育| 融水| 镇宁| 泊头市| 噶尔县| 韶关市| 洛宁县| 包头市| 祁阳县| 中西区| 田东县| 东城区| 彰武县| 同心县| 石家庄市| 奉化市| 岳阳县| 拉萨市| 将乐县| 淅川县| 祁门县| 浠水县| 和硕县| 诸城市| 始兴县| 汉寿县|