jQuery学习笔记(二)

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、空格会影响选择器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值