Jquery find parent element child element of personal experience summary
- 2020-03-30 02:37:21
- OfStack
It is common to find parent and child elements using js or jquery. But always easy to use confusion, here unified summary, after use I believe it will be a lot more convenient
Methods: jquery up here to find the parent element used closest () parents () the parent ()
Find () children()
Js USES the children[] property
The HTML code
Js code:
Methods: jquery up here to find the parent element used closest () parents () the parent ()
Find () children()
Js USES the children[] property
The HTML code
<!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 Find the child element of the parent element </title>
</head>
<body>
<div class="div1" id="div1" name="mydiv">
<p> The paragraph 1 Find the parent element </p>
<table id="table1">
<tbody id="tbody1">
<tr>
<td id="mytd1">11closest() Look up the nearest element (return zero or one element) jQuery Object) </td>
</tr>
<tr id="mytr2">
<td id="mytd2">21parent() methods </td>
</tr>
<tr>
<td id="mytd3">31parent(" The selector ") methods </td>
</tr>
</tbody>
</table>
</div>
<hr>
<div id="div2" style="border-bottom :5px;" name="mydiv">
<p> The paragraph 2 Find child elements </p>
<table id="table2">
<tbody>
<tr>
<td id="sectd1"> To find the table2 the td find() methods </td>
</tr>
<tr id="sectr2">
<td id="sectd2"> To find the table2 the td children() methods </td>
</tr>
<tr>
<td id="sectd3">js the children[] Property to find </td>
</tr>
</tbody>
<tbody>
<tr>
<td>tbody2222</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Js code:
<script type="text/javascript" src="./js/jquery-1.7.2.js"></script>
<script>
$(function(){
//The closest () method
$("#mytd1").bind("click",function(){
//alert($(this).html());
alert($(this).closest("table").attr("id")); //This is table1 instead of table0
//alert($(this).closest("table").html());
});
//The parent () method
$("#mytd2").bind("click",function(){
//alert($(this).html()); //$(this).html() is 21 (this).attr("id") is mytd2
alert($(this).parent().parent().parent().attr("id"));
//.parent() is tr second. Parent is tbody. Even without the tbody tag, the third.parent() of the tbody is the table
//Document.write (" first parent id:" + $(this).parent().attr("id") + ". The second parent id is: "+ $(this). The parent (). The parent (). The attr (" id") + ". The third parent id is: "+ $(this). The parent (). The parent (). The parent (). The attr (" id"));
});
//Parent, parent, parent
$("#mytd3").bind("click",function(){
$("p").parent("#div1").css("background", "yellow");//I've replaced it with a p tag. I don't know why I can't find the element with this
//alert($(this).parent("#div").attr("id"));//undefined
alert($(this).parents("div").attr("id"));//Div1 takes note of one parent parent
});
//Find td element in table2
$("#sectd1").bind("click",function(){
alert($("#table2").find("td").length);
});
//children()
$("#sectd2").bind("click",function(){
var table = $("#table2");
alert($("#table2").children().children().children("td[id='sectd2']").html());
//Children () is tbody children() is tr children("td[id='sectd2']") is td
});
//Js children []
$("#sectd3").bind("click",function(){
var table = document.getElementById("table2");
alert(table.children[0].children[2].children[0].innerHTML);
//Children [0] is tbody children[2] is the third row tr children[0] is td
});
});
</script>