比如需要选择红绿单选框,那么可以使用一个tag name来限制(修饰)class,如下所示:var active_light=
(
“
i
n
p
u
t
.
o
n
”
)
;
当
然
也
可
以
结
合
就
近
的
I
D
,
如
下
所
示
:
v
a
r
a
c
t
i
v
e
l
i
g
h
t
=
" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; ">
(
“
i
n
p
u
t
.
o
n
”
)
;
当
然
也
可
以
结
合
就
近
的
I
D
,
如
下
所
示
:
v
a
r
a
c
t
i
v
e
l
i
g
h
t
=
(
“
i
n
p
u
t
.
o
n
”
)
;
当
然
也
可
以
结
合
就
近
的
I
D
,
如
下
所
示
:
v
a
r
a
c
t
i
v
e
l
i
g
h
t
=
(“#traffic_light input.on”); 如果采用下面的选择器,那么效率是低效的。var traffic_button=
(“#content.button”);因为button已经有ID了,我们可以直接使用ID选择器。如下所示:var traffic_button=
" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; ">
(“#content.button”);因为button已经有ID了,我们可以直接使用ID选择器。如下所示:var traffic_button=
(“#content.button”);因为button已经有ID了,我们可以直接使用ID选择器。如下所示:var traffic_button=
(“#traffic_button”);当然这只是对于单一的元素来讲。如果你需要选择多个元素,这必然会涉及到DOM遍历和循环,为了提高性能,建议从最近的ID开始继承。如下所示:var traffic_lights=$(“#traffic_light input”);
jQuery对于开发者来说有一个很诱人的东西,可以把任何东西挂到
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
下
。
尽
管
" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; ">
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
下
。
尽
管
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
下
。
尽
管
(document).rady确实很有用,它可以在页面渲染时,其它元素还没下载完成就执行。如果你发现你的页面一直是载入中的状态,很有可能就是
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
函
数
引
起
的
。
你
可
以
通
过
将
j
Q
u
e
r
y
函
数
绑
定
到
" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; ">
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
函
数
引
起
的
。
你
可
以
通
过
将
j
Q
u
e
r
y
函
数
绑
定
到
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
函
数
引
起
的
。
你
可
以
通
过
将
j
Q
u
e
r
y
函
数
绑
定
到
(window).load事件的方法来减少页面载入时的cpu使用率。它会在所有的html(包括iframe)被下载完成后执行。一些特效的功能,例如拖放,视觉特效和动画,预载入隐藏图像等等,都是适合这种技术的场合。
jQuery选择器中有一个这样的选择器,它能指定上下文。jQuery(expression,context);通过它,能缩小选择器在DOM中搜索的范围,达到节省时间,提高效率。普通方式:
(
‘
.
m
y
D
i
v
′
)
改
进
方
式
:
" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; ">
(
‘
.
m
y
D
i
v
′
)
改
进
方
式
:
(
‘
.
m
y
D
i
v
′
)
改
进
方
式
:
(‘.myDiv’,$(“#listItem”))。