当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JQuery 学习笔记 选择器之四

Javascript
[JS源码]超长文章自动分页(客户端版)
注意 JavaScript 中 RegExp 对象的 test 方法
JavaScript快速排序
写的一段拖动对象的代码
用js取得鼠标所在位置的对象
如何在一段文字里点一下就可以在里面插入一段文字?
用js+cookie记录滚动条位置
记录滚动条位置(使用userdate)
强效、方便的表单通用检测JS 不错
个人总结的一些关于String、Function、Array的属性和用法
数据排序谁最快(javascript中的Array.prototype.sort PK 快速排序)
Prototype最新版(1.5 rc2)使用指南(1)
Prototype使用指南之string.js
Prototype使用指南之base.js
Prototype使用指南之enumerable.js
Prototype使用指南之array.js
Prototype使用指南之range.js
Prototype使用指南之ajax
Prototype使用指南之dom.js
Prototype使用指南之selector.js

Javascript 中的 JQuery 学习笔记 选择器之四


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 270 ::
收藏到网摘: n/a

以下是测试例子,这边测试例子不再像之前几章以显示HTML与ID的方式来查看结果,换以改变红色背景颜色来查看,应该会更直观点吧^^
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="js/jquery-1.3.2.js"></script>
<script type="text/javascript"><!--
$(function(){
$("#aContains").click(function(){
$("div:contains('hello')").each(function(){
$(this).css("background","red");
})
})
$("#aEmpty").click(function(){
$("div:empty").each(function(){
$(this).html("EmptyDIV");
})
})
$("#aHas").click(function(){
$("div:has(p)").each(function(){
$(this).css("background","red");
})
})
$("#aParent").click(function(){
$("div:parent").each(function(){
$(this).css("background","red");
})
})
})
// --></script>
</head>
<body>
<div id ="div1">
<p>hello word!</p>
</div>
<div id ="div2">
hello
</div>
<div id ="div3" height="20px">
</div>
<div id ="div4"></div>
<a href="#" id="aContains">设置内容包含“hello”的节点红色背景颜色</a>
<a href="#" id="aEmpty">设置无内容的DIV内容设为EmptyDIV</a>
<a href="#" id="aHas">设置包含 p 节点的 div 节点红色背景颜色</a>
<a href="#" id="aParent">设置包含子节点的 div 节点红色背景颜色</a>
</body>
</html>

首先还是对本章的课外知识点进行说明下
1.element.css("attributeName","value")
描述:用于设置element的style,在例子中$(this).css("background","red");就是设置节点的background为red。
现在进入主题咯
1.$("TagName:contains('keyword')")
描述:用于获取所有TagName节点里包含keyword内容的节点集合
返回值:Array(Element);
2.$("TagName:Empty")
描述:用于获取所有TagName节点里,内容为空的节点集合
返回值:Array(Element);
3.$("TagName1:has(TagName2))
描述:用于获取所有TagName1节点里,包含TagName2子节点的节点集合
返回值:Array(Element);
4.$("TagName:parent")
描述:用于获取所有TagName节点里,包含子节点的节点集合
返回值:Array(Element);