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、空格会影响选择器
$()函数类似于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、空格会影响选择器
相关文章推荐
- jQuery选中该复选框来实现/全部取消/未选定/获得的选定值
- jquery getjson方法在ie下失效的问题
- jQuery改变label/input的值,改变class,改变img的src
- 关Jquery判断input type="checkbox"元素是否被选中的判断
- jQuery实现向下滑出的平滑下拉菜单效果
- jQuery学习笔记(一)
- JQuery插件开发之如何实时显示被隐藏的输入框密码
- Cookie记住密码
- jQuery实现select三级联动
- js和jquery如何获取图片真实的宽度和高度
- jQuery实现导航栏
- jQuery实现点击式选项卡
- jQuery对象和dom对象
- jQuery插件
- jQuery操作Ajax
- jQuery动画与特效
- jQuery操作dom事件
- jQuery操作dom
- jQuery选择器
- jQuery初步