jQuery 遍历
jQuery 遍历 - 祖先
jQuery parent() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery parent() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="parent">
<div id="child">
<p>这是一个子元素。</p>
</div>
<div id="sibling">
<p>这是一个同级元素。</p>
</div>
</div>
<p>这是一个父元素。</p>
<p>这是另一个父元素。</p>
<div id="grandparent">
<div id="great-grandparent">
<div id="great-great-grandparent">
<p>这是一个伟大的曾祖元素。</p>
</div>
</div>
</div>
<p>这是另一个父元素。</p>
<p>这是一个父元素</p>
<p>这是另一个父元素。</p>
<button id="btn">获取child元素的父元素</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var parent = $("#child").parent();
alert(parent.attr("id"));
});
});
</script>
</body>
</html>
jQuery parents() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery parents() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<div id="inner">
<p>This is the inner div</p>
</div>
<p>This is the outer div</p>
</div>
<button id="btn">Click me</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var innerDiv = $("#inner");
var outerDiv = innerDiv.parents("#container");
alert(outerDiv.html());
});
});
</script>
</body>
</html>
jQuery parentsUntil() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery parentsUntil() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<div id="child1">
<div id="grandchild1">
<p>This is the first grandchild</p>
</div>
</div>
</div>
<div id="sibling1">
<p>This is the first sibling</p>
</div>
<div id="sibling2">
<p>This is the second sibling</p>
</div>
<div id="sibling3">
<p>This is the third sibling</p>
</div>
<div id="sibling4">
<p>This is the fourth sibling</p>
</div>
<div id="sibling5">
<p>This is the fifth sibling</p>
</div>
<button id="btn">Get Parents Until</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var $parents = $("#child1").parentsUntil("#container");
console.log($parents);
});
});
</script>
</body>
</html>
jQuery 遍历 - 后代
jQuery children() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery children() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<div>
<p>This is the third paragraph.</p>
<p>This is the fourth paragraph.</p>
</div>
</div>
<button id="btn">获取子元素</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var children = $("#container").children();
console.log(children);// 输出:init {0: p, 1: p, 2: div, length: 3, prevObject: init, context: document}
});
});
</script>
</body>
</html>
jQuery find() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery find() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
<p>This is the fourth paragraph.</p>
<p>This is the fifth paragraph.</p>
</div>
<button id="btn">Click me</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var p = $("#container").find("p");
console.log(p);//获取所有p标签,输出结果为:init {0: p, 1: p, 2: p, 3: p, 4: p, length: 5, prevObject: init, context: document, selector: '#container p'}
});
});
</script>
</body>
</html>
jQuery 遍历 - 同胞(siblings)
jQuery siblings() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery siblings() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="parent">
<div id="child1">Child 1</div>
<div id="child2">Child 2</div>
<div id="child3">Child 3</div>
</div>
<button id="btn1">Prev Sibling</button>
<button id="btn2">Next Sibling</button>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#child2").siblings().css("background-color","red");
});
$("#btn2").click(function(){
$("#child2").nextAll().css("background-color","green");
});
});
</script>
</body>
</html>
jQuery next() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery next() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="parent">
<div id="child1">Child 1</div>
<div id="child2">Child 2</div>
<div id="child3">Child 3</div>
</div>
<button id="btn">Next</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$("#child1").next().css("background-color","red");
});
});
</script>
</body>
</html>
jQuery nextAll() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery nextAll() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
<p>This is the fourth paragraph.</p>
</div>
<p>This is the fifth paragraph.</p>
<p>This is the sixth paragraph.</p>
<button id="btn">获取下一个元素</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var next = $("#btn").nextAll();
console.log(next);
});
});
</script>
</body>
</html>
jQuery nextUntil() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery nextUntil() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
<p>This is the fourth paragraph.</p>
<p>This is the fifth paragraph.</p>
<p>This is the sixth paragraph.</p>
<p>This is the seventh paragraph.</p>
<p>This is the eighth paragraph.</p>
<p>This is the ninth paragraph.</p>
</div>
<br>
<button id="btn">Next Until</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var $container = $("#container");
var $next = $container.nextUntil("p:last");//获取直到最后一个p标签之前的所有元素
console.log($next);
$next.css("background-color","yellow");//设置背景色为黄色
});
});
</script>
</body>
</html>
jQuery 遍历- 过滤
jQuery first() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery first() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
<p>jQuery first() 方法</p>
<p>jQuery first() 方法用于选取第一个匹配元素。</p>
<p>语法:$(selector).first()</p>
<p>例子:</p>
<p>获取第一个<p>元素:</p>
<pre>
$(selector).first();
</pre>
<p>获取第一个<div>元素的第一个<p>元素:</p>
<pre>
$(selector).first().first();
</pre>
</div>
<script>
$(document).ready(function(){
//获取第一个<p>元素
var firstP = $("p").first();
console.log(firstP);
//获取第一个<div>元素的第一个<p>元素
var firstDivP = $("div").first().first();
console.log(firstDivP);
});
</script>
</body>
</html>
jQuery last() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery last() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>第一段</p>
<p>第二段</p>
<p>第三段</p>
<p>第四段</p>
</div>
<button id="btn">获取最后一个段落</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var lastP = $("#container p:last");
alert(lastP.text());
});
});
</script>
</body>
</html>
jQuery eq() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery eq() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>第一段</p>
<p>第二段</p>
<p>第三段</p>
<p>第四段</p>
<p>第五段</p>
</div>
<button id="btn">点我</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var $p = $("#container p:eq(2)"); //获取第3个p元素
$p.text("修改后的段落"); //修改第3个p元素的文本
});
});
</script>
</body>
</html>
jQuery filter() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery eq() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div id="container">
<p>第一段</p>
<p>第二段</p>
<p>第三段</p>
<p>第四段</p>
<p>第五段</p>
</div>
<button id="btn">点我</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var $p = $("#container p:eq(2)"); //获取第3个p元素
$p.text("修改后的段落"); //修改第3个p元素的文本
});
});
</script>
</body>
</html>
jQuery not() 方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery HTML -jQuery not() 方法</title>
<script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<div>
<p>Hello, world!</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
<p>jQuery not() 方法</p>
</div>
<button id="btn">点击</button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$("p:not(:first)").hide();
});
});
</script>
</body>
</html>