Home  >  Article  >  Web Front-end  >  jquery selector part organization_jquery

jquery selector part organization_jquery

WBOY
WBOYOriginal
2016-05-16 18:43:091099browse

这个是网页版,直接用ctrl+F搜索,如果大家不是经常上网,可以用chm版的更方便些。
jQuery 1.3 API 参考手册 CHM

锋利的jquery 电子书

近期脚本之家将会将一些jquery学习教程逐步发一些。参考了锋利的jquery一书。

$的选择器部分:
凡是运用$,其返回值是一个object
$选择器主要用于选择标签.基本用法是同css的选择器.但是,很让人兴奋的是,他支持常见的浏览器,而css中很多选择器是IE6所不支持的.
1.基本选择器(3种):
$("标签名"),如$("p")是选取了所有的p标签节点
$("#id名"),如$("#test")是选取了id为test的标签节点
$(".class名"),如$(".test")是选取了所有class为test的标签节点
上面的$("标签名")和$(".class名")返回的都是所有满足的节点,至于进一步筛选可以添加一些函数,如eq,gt,lt等等.
2.组选择器:
下面还是现做一个约定:把"标签名或#id名或.class名"记作mix,则mix表示一个标签名,或一个#id或一个.class.
$("mix,mix,mix,..."),如:$("div,#test1,p,.test2,#test3")

3.后代选择器:
$("mix mix"),当然可以是多个嵌套,但后代选择器可以是深层子代,所以$("mix mix mix ...")这种写法作用不大.例子:$("div .test"):在div标签内的所有具有test的class的后代元素(就是被div嵌套的class属性为test的标签)
可以见DEMO。

4.子选择器:
$("mix>mix"),这个放在后代选择器后面是为了和它做对比.子选择器只能选择第一代子代.不处理深层嵌套.例子:
$("div>.test")

对这里的p段落标签有效.但对

对这里的p段落标签无效,这里要用
$("div .test)

5.临近选择器:
$("mix+mix"),选取下一个兄弟节点.如:$("div +#test"),id为test的的节点必须是div的下一个兄弟节点.

在$("div + #test")中能取到p段落节点

则不能取到

6.属性选择器:
把属性选择器不放在css选择器里面是因为jQuery中写法是不一样的.至于css中写法可以参考我之前写的一篇css的选择器一文.jQuery中是和xPath类似的写法:
$("mix[@attr]"):选取所有该mix且具有attr属性的节点
$("mix[@attr=a_value"]):选取所有该mix且具有attr属性并满足属性值为a_value的节点
$("mix[@attr^=a_value_head"]):attr属性的属性值是以a_value_head开头的
$("mix[@attr$=a_value_end"]):attr属性的属性值是以a_value_end结尾的
$("mix[@attr*=a_value"]):attr属性的属性值中包含a_value

7.进一步选择器:
这个名称是我自己起的,其实选择器组合都有进一步的意思,你明白后面所介绍的知识即可.
具有限定子节点选择器:$("mix1[mix2]"):返回包含mix2的mix1节点.如:$("div[a]"):包含a标签的div.
这个和$("div a")不相同.后者表示div中的a标签,返回的是a标签对象,前者返回的是div标签对象
冒号限定结点选择器:$("mix:condition"):mix标签,并且满足限定条件.
E:root:类型为E,并且是文档的根元素
E:nth-child(n):是其父元素的第n个类型为E的子元素 ,基数从1开始
E:first-child:是其父元素的第1个类型为E的子元素
E:last-child:是其父元素的最后一个类型为E的子元素
E:only-child:且是其父元素的唯一一个类型为E的子元素
E:empty:没有子元素(包括text节点)的类型为E的元素
E:enabled
E:disabled:类型为E,允许或被禁止的用户界面元素
E:checked:类型为E,处于选中状态的用户界面元素(例如单选按钮或复选框)
E:visible:选择所有可见元素(display值为block或visible,visibility值为visible元素,不包括hide域)
E:hidden:选择所有隐藏元素(非Hide域,且display值为block或visible,visibility值为visible的元素)
E:not(s):类型为E,不匹配选择器s
E:eq(n),E:gt(n),E:lt(n):元素限定
E:first:相当于E:eq(0)
E:last:最后一个匹配的元素
E:even:从匹配的元素集中取序数为偶数的元素
E:odd:从匹配的元素集中取序数为奇数的元素
E:parent:选择包含子元素(包含text节点)的所有元素
E:contains('test'):选择所有含有指定文本的元素
表单选择器:
E:input:选择表单元素(input,select,textarea,button)
E:text:选择所有文本域(type="text")
E:password:选择所有密码域(type="password")
E:radio:选择所有单选按钮(type="radio")
E:checkbox:选择所有复选框(type="checkbox")
E:submit:选择所有提交按钮(type="submit")
E:image:选择所有图像域 (type="image")
E:reset:选择所有清除域(type="reset")
E:button:选择所有按钮(type="button")
当然包括E:hidden

8.xPath路径查询:

先介绍下xPath的语法:
/:选取根节点
//:选取文档中所有符合条件的节点,不管该节点位于何处
.:选取当前节点
..:选取单前节点的父节点
@:选取属性,这个在之前说过了(属性选择器)
nodename:选取节点下的所有节点
jQuery中的应用:
根节点是很少用到的,常用的如下面的例子:
$("div/p")相当于$("div>p")
$("div//p")相当于$("div p")
$("//div/../p"):所有div节点的父节点下的p标签
还有相对路径的写法以及支持的Axis选择器,还不是会应用,不介绍了...已经一大堆了

$的其他用法:

$(html节点):根据提供的原始HTML标记字符串,动态创建由jQuery对象包装的DOM元素.如:
$("

Hello

").appendTo("#body");//把

Hello

Added to the body element
$(document): web page document object
$(document.body): web page body object, which is the same as $("body")
$(function): DOM The function is executed after loading. So $(document).ready() can be written as $()
$(selector part, selector source): This example illustrates
$("input:radio" ,document.forms[0]): In the first form of the document, search all radio buttons
$("div",xml.responseXML): Query all div elements
in the specified XML document The source of the handler can be: DOM element, document or jQuery object as context
There are two more: $.extend(prop) and $.noConflict() are compatible with plug-ins and other libraries, and will be written later

The core part of jQuery also includes:

eq (number): Reduce the set of matched elements to one element. The position of this element in the set of matching elements becomes 0, and the length of the set becomes 1
gt (number): Reduce the set of matching elements to all elements after the given position
lt (number): Reduce The set of matching elements is reduced to all elements before the given position
Examples of the above three:
$("div:eq(1)")//The second div
$("div: gt(2)")//The third div and the following div
$("div:lt(2)")//The second div and the previous div, that is, the first div and the second div

length or size(): the number of currently matched elements

each(): execute a function with each matched element as the context. This means that each time the function passed in is executed, the this keyword in the function points to a different element (a different matching element each time). Moreover, each time the function is executed, the function is passed A numeric value representing the position of the element that is the execution environment in the set of matching elements is used as a parameter.
$("img").each(function(i){ this.src = "test" i ".jpg "; });//Iterate the images and set their src attributes

get(): If there are no parameters, return all, which is an object array; if there are parameters, they must be numbers, and the base starts from 0 .Example:
$("div").get(): Returns an array of div objects
$("div").get(1): Returns the second div object
index (required Element node object): Returns a number. Use an example to illustrate:
$("div").index($(".test"))[1] //Indicates finding the class attribute with test from all div nodes node. And what we are looking for is the second node (the base starts from 0). The return value is the position of the node in the div node (the base also starts from 0).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn