当前位置:嗨网首页>书籍在线阅读

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>