亲宝软件园·资讯

展开

jQuery遍历DOM jQuery常见的遍历DOM操作详解

furway 人气:0

本文实例总结了jQuery常见的遍历DOM操作。分享给大家供大家参考,具体如下:

向上遍历DOM树

<!DOCTYPE html>
<html>
<head>
<style>
.ancestors *{
display:block;
border:2px solid lightgrey;
color:lightgrey;
padding:5px;
margin:15px;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script >
$(document).ready(function(){
$("span").parent().css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body>
<div class="ancestors">
<div style="width:500;">div(曾祖父)
<ul>ul(祖父)
<li>li(直接父)
<span>span</span>
</li>
</ul>
</div>
<div style="width:500px;">div(祖父)
<p>p(直接父)
<span>span</span>
</p>
</div>
</div>
</body>
</html>

运行结果:

parentsUntil()方法

$(document).ready(function(){
$("span").parentsUntil("div");
});

<!DOCTYPE html>
<html>
<head>
<style>
.ancestors *{
display:block;
border:2px solid lightgrey;
color:lightgrey;
padding:5px;
margin:15px;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body class="ancestors">body(增曾祖父)
<div style="width:500px;">div(曾祖父)
<ul>ul(祖父)
<li>li(直接父)
<span>span</span>
</li>
</ul>
</div>
</body>
</html>

运行结果:

向下遍历DOM树

children()方法

<!DOCTYPE html>
<html>
<head>
<style>
.descendants *{
display:block;
border:2px solid lightgrey;
color:lightgrey;
padding:5px;
margin:15px;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").children().css({"color":"red","border":"2px solid red"});
$("div").children("p.1").css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div(当前元素)
<p class="1">p(子)
<span>span(孙)</span>
</p>
<p class="2">p(子)
<span>span(孙)</span>
</p>
</div>
</body>
</html>

运行结果:

find()方法

<!DOCTYPE html>
<html>
<head>
<style>
.descendants *{
display:block;
border:2px solid lightgrey;
color:lightgrey;
padding:5px;
margin:15px;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").find("span").css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div(current element)
<p>P子
<span>span(孙)</span>
</p>
<p>p子
<span>span(孙)</span>
</p>
</div>
</body>
</html>

运行结果:

返回<div>所有后代

$(document).ready(function(){
$("div").find("*");
});

水平遍历DOM树

<!DOCTYPE html>
<html>
<head>
<style>
.siblings *{
display:block;
border:2px solid lightgrey;
color:lightgrey;
padding:5px;
margin:15px;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h2").siblings().css({"color":"red","border":"2px solid red"});
});
</script>
</head>
<body class="siblings">
<div>div(父)
<p>p</p>
<span>span</span>
<h2>h2</h2>
<h3>h3</h3>
<p>p</p>
</div>
</body>
</html>

运行结果:

jQuery遍历 过滤

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").first().css("background-color","yellow");
});
</script>
</head>
<body>
<h1>我心在北朝、</h1>
<div>
<p>田野上</p>
</div>
<div>
<p>红彤彤的野花</p>
</div>
<p>玲珑剔透</p>
</body>
</html>

运行结果:

eq()方法的使用

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").eq(1).css("background-color","yellow");
});
</script>
</head>
<body>
<h1>我心在南朝、</h1>
<p>田野上</p>
<p>红彤彤的野花</p>
<p>玲珑剔透</p>
<p>我爱你</p>
</body>
</html>

运行结果:

filter()方法的使用

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color","yellow");
});
</script>
</head>
<body>
<h1>我心在南朝、</p>
<p>田野上</p>
<p class="intro">红彤彤的草莓</p>
<p class="intro">玲玲剔透</p>
<p>我爱你</p>
</body>
</html>

运行结果:

希望本文所述对大家jQuery程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论