您的位置:首页 > Web前端 > JQuery

jQuery学习笔记(二)

2015-08-21 20:20 639 查看
jQuery的$()函数

$()函数类似于getElementById()、getElementByTagName()

但是当使用getElemById等函数时,一旦对象不存在,就会报错。

而$()即使获取网页中不存在的元素也不会报错。

因此要判断一个元素在网页上是否存在时,

$()函数应该使用if ( $("tt").length > 0 )的方法来判断。

jQuery选择器

jQuery选择器原理和CSS选择器的原理一样。主要包含以下几种选择器:

(1)基本选择器

通过元素id、class和标签名来查找元素;

(2)层次选择器

通过DOM元素之间的层次关系来获取特定元素,如:

$("ancestor descendant"):选取ancestor元素里的所有descendant后代元素

$("parent > child"):选取parent元素下的子元素

$("prev + next"):选取紧接在prev元素后的next元素——等价于$(".one").next("div")

$("prev ~ siblings"):选取prev元素之后的所有siblings元素——等价于$("#prev").nexAllt("div")

(3)过滤选择器

通过特定的过滤规则来筛选出所需的DOM元素,过滤规则与CSS中的伪类选择器语法相同,即选择器都以一个冒号(:)开头。可分为基本过滤、内容过滤、可见性过滤、属性过滤、子元素过滤、表单对象过滤选择器。

1、基本过滤选择器

:first

:last

:not(selector)

:even

:odd

:eq(index)

:gt(index)

:lt(index)

:header

:animated

:focus

2、内容过滤选择器

:contains(text)

:empty

:has(selector)

:parent

3、可见性过滤选择器

:hidden

:visible

4、属性过滤选择器

[attribute]

[attribute=value]

[attribute!=value]

[attribute^=value]

[attribute$=value]

[attribute*=value]

[attribute|=value]

[attribute~=value]

[attribute1][attribute2][attributeN]

5、子元素过滤选择器

:nth-child(index/even/odd/equation)——用法::nth-child(even)、:nth-child(2)、:nth-child(3n+1)

:first-child

:last-child

:only-child

6、表单对象属性过滤选择器

:enabled

:disabled

:checked

:selected

7、表单选择器

:input

:text

:password

:radio

:checkbox

:submit

:image

:reset

:button

:file

:hidden

选择器中含有特殊符号的注意事项

1、选择器中含有 . # ( ]等特殊字符,需要进行转义\\# \\]

2、空格会影响选择器
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: