36-解决方案
1.8.2 解决方案
可以用 andSelf()
方法合并前一个DOM元素选择集和当前选择集。例如,在下面的代码中,首先选择页面上的所有 <div>
元素。接下来,操纵这组元素,寻找 <div>
元素中的所有 <p>
元素。现在,为了同时操作 <div>
和 <div>
中找到的 <p>
元素,可以用 andSelf()
方法将 <div>
包含到当前集合。如果省略 andSelf()
,边框颜色将只应用到 <p>
元素:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<p>Paragraph</p>
<p>Paragraph</p>
</div>
<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
jQuery('div').find('p').andSelf().css('border','1px solid #993300');
</script>
</body>
</html>