菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
377
0

jquery查找父元素、子元素(个人经验总结)

原创
05/13 14:22
阅读数 82919
jquery








复制代码 代码如下:

<!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>
</htm编程客栈l>


复制代码 代码如下:

<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

//docum编程客栈ent.write("第一个parent的id:" + $(this).parent().attr("id") + "。 第二个parent的id是:"+$(this).parent().parent().attr("id") + "。 第三个parent的id是:"+$(this).pa编程客栈rent().parent().parent().attr("id"));

});

//parent("选择器") parents("选择器")
$("#mytd3").bind("click",function(){
$("p").pawww.cppcns.comrent("#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().childre编程客栈n("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查找父元素、子元素(个人经验总结)
本文地址: http://www.cppcns.com/wangluo/javascript/107298.html

发表评论

0/200
377 点赞
0 评论
收藏
为你推荐 换一批