jQuery相关精选文章.docx
- 文档编号:18609771
- 上传时间:2023-08-20
- 格式:DOCX
- 页数:74
- 大小:572.32KB
jQuery相关精选文章.docx
《jQuery相关精选文章.docx》由会员分享,可在线阅读,更多相关《jQuery相关精选文章.docx(74页珍藏版)》请在冰点文库上搜索。
jQuery相关精选文章
一些应该熟记于心的jQuery函数和技巧
高级选择器(selector)
在jQuery中,我们可以使用各种各样的选择器,这使得选择器的使用变得非常精确。
51CTO也曾经和读者分享过jQuery选择器深入分析:
避免不必要的调用,下面我们来一步一步地讲解这些选择器并看看在其他语境中如何使用这些选择器。
基于属性的选择器
在HTML中,几乎所有元素都具有属性,比如:
1
2
上面两个HMTL元素中包含了九个属性。
利用jQuery,我们可以根据元素的属性和属性值来对元素进行选择。
一起看看以下例子中的选择器:
3$(document).ready(function(){
4//Alltheimageswhosewidthis600px
$("img[width=600]").click(function(){
5alert("You'vejustselectedanimagewhosewidthis600px");
6});
7//Alltheimageshavingawidthdifferentto600px
8$("img[width!
=600]").click(function(){
9alert("You'vejustselectedanimagewhosewidthisnot600px");
10});
11//Alltheinputswhosenameendswith'email'
12$("input[name$='email']").focus(function(){
13alert("Thisinputhasanamewhichendswith'email'.");
14});
15});
在官方文档中,我们可以看到许多选择器与上例中的选择器非常类似。
但关键点是一样的,属性和属性值。
多重选择器
如果你开放的是一个较为大型的网站,选择器的使用会变得困难。
有时为了让代码变得更为简单,最好将它们分割为两个或三个选择器。
实际上这是非常简单和基础的知识,不过非常有用,我们应该把这些知识熟记于心。
16$(document).ready(function(){
17//Alltheimageswhosewidthis600pxORheightis400px
18$("mon[width=600],mon[height=400]").click(function(){
19alert("Selectedanimagewhosewidthis600pxORheightis400px");
20});
21//Allpelementswithclassorange_text,divsandimages.
22$("p.orange_text,div,img").hover(function(){
23alert("Selectedapelementwithclassorange_text,adivORanimage.");
24});
25//Wecanalsocombinetheattributesselectors
26//Allthejpgimageswithanaltattribute(thealt'svaluedoesn'tmatter)
27$("img[alt][src$='.jpg']").click(function(){
28alert("Youselectedajpgimagewiththealtattribute.");
29});
30});
Widget组件选择器
除了插件的选择器之前,还有一些基于元素某些属性或位置的选择器。
下面让我们看看这些更为重要的选择器:
1$(document).ready(function(){
2//Allthehiddenimagesareshown
3$("img:
hidden").show();
4//Thefirstpisgoingtobeorange
5$("p:
first").css("color","orange");
6//Inputwithtypepassword
7//thisis$("input[type='password']")
8$("input:
password").focus(function(){
9alert("Thisisapassword!
");
10});
11//Divswithparagraph
12$("div:
has(p)").css("color","green");
13//Wecanalsocombinethem.with()
14//Allnotdisabledcheckboxes
15$("input:
checkbox(:
not(:
disabled))").hover(function(){
16alert("Thischeckboxisworking.");
17});
18});
如上例所示,可供使用的选择器是多种多样的,并且它们之前不是互相独立的,所以我们可以将这些选择器组合起来进行使用,如上例中的选择器。
理解网站的结构
网络的结构可能看起来非常复杂,但事实上并非如此,如果我们想要使用某些选择器以及作用于网络结构之上的方法。
我们可以将网站视为一个颠倒的树,树根在顶部,而其他元素从根部延伸出来。
查看下面的代码,试着想象一棵树,树根是body标签。
19 //www.w3.org/1999/xhtml"> 20
21
22
23
24
CreateanAccount!
25
26
27
28
29
30
Message
31
--Endmain-->
32
--Endwrapper-->
33
34
Footermessage
35
--Endfooter-->
36
37