javascript 第27节 jQuery选择器
下面的html需要以下2个文件:
1.style.css
div,span,p { width:140px; height:140px; margin:5px; background:#aaa; border:#000 1px solid; float:left; font-size:17px; font-family:Verdana; } div.mini { width:55px; height:55px; background-color: #aaa; font-size:12px; } div.hide { display:none; }
2.main.js
$(function () { function moveSpan() { $("#mover").slideToggle(3000, moveSpan); } moveSpan(); })
3.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>基本选择器</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="jquery-1.7.2.js"></script> <script type="text/javascript"> $(function () { $("#but1").click(function() { $("#one").css("background-color","#bfa"); }); $("#but2").click(function() { $(".one").css("background-color","#bfa"); }); $("#but3").click(function() { $("div").css("background-color","#bfa"); }); $("#but4").click(function() { $("*").css("background-color","#bfa"); }); $("#but5").click(function() { $("#one,.mini").css("background-color","#bfa"); }); }) </script> </head> <body> <h3>基本选择器.</h3> <!-- 控制按钮 --> <input type="button" value="id是one的元素" id="but1" /> <input type="button" value="class是one的元素" id="but2" /> <input type="button" value="div元素" id="but3" /> <input type="button" value="所有元素" id="but4" /> <input type="button" value="id是one,class是mini" id="but5" /> <br /><br /> <!-- 测试的元素 --> <div class="one" id="one" > id为one,class为one的div <div class="mini">class为mini</div> </div> <div class="one" id="two" title="test" > id为two,class为one,title为test的div. <div class="mini" title="other">class为mini,title为other</div> <div class="mini" title="test">class为mini,title为test</div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini"></div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini" title="tesst">class为mini,title为tesst</div> </div> <div style="display:none;" class="none"> style的display为"none"的div </div> <div class="hide">class为"hide"的div</div> <div> 包含input的type为"hidden"的div<input type="hidden" size="8"/> </div> <span id="mover">span元素.</span> </body> </html>
rs:
4.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>基本过滤选择器</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="jquery-1.7.2.js"></script> <script type="text/javascript" src="main.js"></script> <script type="text/javascript"> $(function () { $("#but1").click(function() { $("div:first").css("background-color","#bfa"); }); $("#but2").click(function() { $("div:last").css("background-color","#bfa"); }); $("#but3").click(function() { $("div:not(#one)").css("background-color","#bfa"); }); $("#but4").click(function() { $("div:eq(3)").css("background-color","#bfa"); }); $("#but5").click(function() { $("div:odd").css("background-color","#bfa"); }); $("#but6").click(function() { $("div:even").css("background-color","#bfa"); }); $("#but7").click(function() { $(":header").css("background-color","#bfa"); }); $("#but8").click(function() { $(":animated").css("background-color","#bfa"); }); }) </script> </head> <body> <h3>基本过滤选择器.</h3> <!-- 控制按钮 --> <input type="button" value="第一个div元素" id="but1" /> <input type="button" value="最后一个div元素" id="but2" /> <input type="button" value="不包含id=one的div元素" id="but3" /> <input type="button" value="索引是3的div元素" id="but4" /> <input type="button" value="索引是奇数div元素" id="but5" /> <input type="button" value="索引是偶数div元素" id="but6" /> <input type="button" value="标题元素" id="but7" /> <input type="button" value="动画元素" id="but8" /> <br /><br /> <!-- 测试的元素 --> <div class="one" id="one" > id为one,class为one的div <div class="mini">class为mini</div> </div> <div class="one" id="two" title="test" > id为two,class为one,title为test的div. <div class="mini" title="other">class为mini,title为other</div> <div class="mini" title="test">class为mini,title为test</div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini"></div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini" title="tesst">class为mini,title为tesst</div> </div> <div style="display:none;" class="none"> style的display为"none"的div </div> <div class="hide">class为"hide"的div</div> <div> 包含input的type为"hidden"的div<input type="hidden" size="8"/> </div> <span id="mover">span元素.</span> </body> </html>
rs:
5.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>层次选择器</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="jquery-1.7.2.js"></script> <script type="text/javascript"> $(function () { $("#but1").click(function() { $("body div").css("background-color","#bfa"); }); $("#but2").click(function() { $("body>div").css("background-color","#bfa"); }); $("#but3").click(function() { $("#one + div").css("background-color","#bfa"); }); $("#but4").click(function() { $("#one ~ div").css("background-color","#bfa"); }); }) </script> </head> <body> <h3>层次选择器.</h3> <!-- 控制按钮 --> <input type="button" value="body下div元素" id="but1" /> <input type="button" value="body下div子元素" id="but2" /> <input type="button" value="id是one的后div元素" id="but3" /> <input type="button" value="id是one的后所有div元素" id="but4" /> <br /><br /> <!-- 测试的元素 --> <div class="one" id="one" > id为one,class为one的div <div class="mini">class为mini</div> </div> <div class="one" id="two" title="test" > id为two,class为one,title为test的div. <div class="mini" title="other">class为mini,title为other</div> <div class="mini" title="test">class为mini,title为test</div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini"></div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini" title="tesst">class为mini,title为tesst</div> </div> <div style="display:none;" class="none"> style的display为"none"的div </div> <div class="hide">class为"hide"的div</div> <div> 包含input的type为"hidden"的div<input type="hidden" size="8"/> </div> <span id="mover">span元素.</span> </body> </html>
rs: