這裏是jsfiddle,exalains的問題,http://jsfiddle.net/4CuqR/爲什麼鏈接.andSelf()不起作用?
複製粘貼相同的小提琴代碼在這裏。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
p,div { margin: 0; padding: 0; padding-left: 20px; }
</style>
</head>
<html>
<body>
<DIV class="main">
<span>Main Class</span>
<DIV class="sub1">
<span>sub1 Class</span>
<p>Hello P1</p>
<p>Hello P2</p>
</DIV>
</DIV>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function() {
$('.main').children('.sub1').children('p:first').andSelf().andSelf().css("color", "red");
});
</script>
</html>
在上面的代碼中,理想地DIV.main也應該是紅色。
這裏是輸出上面的代碼。