This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14058|Reply: 0

[Jquery] Several simple jquery practice questions with additional answers

[Copy link]
Posted on 8/15/2015 10:42:50 AM | | | |


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title></title>
  5.         <style type="text/css">
  6.                 div,span,p {
  7.                         width:160px;
  8.                         height:160px;
  9.                         margin:5px;
  10.                         background:#aaa;
  11.                         border:#000 1px solid;
  12.                         float:left;
  13.                         font-size:17px;
  14.                         font-family:Verdana;
  15.                 }
  16.                 div .mini {
  17.                         width:55px;
  18.                         height:55px;
  19.                         background-color: #aaa;
  20.                         font-size:12px;
  21.                 }
  22.                 fieldset
  23.                 {
  24.                          border-style:none;
  25.                 }
  26.         </style>
  27.         <scrip去掉t type="text/javascrip去掉t" src="jquery-1.4.1-vsdoc.js"></scrip去掉t>
  28.         <scrip去掉t type="text/javascrip去掉t">
  29.             function a1()
  30.                 {
  31.                         $("#one").css("background-color","red");
  32.                 }
  33.                 function a2()
  34.                 {
  35.                         $(".mini").css("background-color","red");
  36.                 }
  37.                 function a3()
  38.                 {
  39.                         $("div").css("background-color","red");
  40.                 }
  41.                 function a4()
  42.                 {
  43.                         $("*").css("background-color","red");
  44.                 }
  45.                 function a5()
  46.                 {
  47.                         $("span,#two").css("background-color","red");
  48.                 }
  49.                 function b6()
  50.                 {
  51.                         $("body div").css("background-color","red");
  52.                 }
  53.                 function b7()
  54.                 {
  55.                         $("body>div").css("background-color","red");
  56.                 }
  57.                 function b8()
  58.                 {
  59.                         $(".one+div").css("background-color","red");
  60.                 }
  61.                 function b9()
  62.                 {
  63.                         $("#two~div").css("background-color","red");
  64.                 }
  65.                 function b8_a()
  66.                 {
  67.                         $(".one+div[class!=one]").css("background-color","red");
  68.                 }
  69.                 function c10()
  70.                 {
  71.                         $("div:first").css("background-color","red");
  72.                 }
  73.                 function c11()
  74.                 {
  75.                         $("div:last").css("background-color","red");
  76.                 }
  77.                 function c12()
  78.                 {
  79.                         $("div:not(.one)").css("background-color","red");
  80.                 }
  81.                 function c13()
  82.                 {
  83.                         $("div:even").css("background-color","red");
  84.                 }
  85.                 function c14()
  86.                 {
  87.                         $("div:odd").css("background-color","red");
  88.                 }
  89.                 function c15()
  90.                 {
  91.                         $("div:eq(3)").css("background-color","red");
  92.                 }
  93.                 function c16()
  94.                 {
  95.                         $("div:gt(3)").css("background-color","red");
  96.                 }
  97.                 function c17()
  98.                 {
  99.                         $("div:lt(3)").css("background-color","red");
  100.                 }
  101.                 function c18()
  102.                 {
  103.                         $(":header").css("background-color","red");
  104.                 }
  105.                 function d21()
  106.                 {
  107.                         $("div:contains('di')").css("background-color","red");
  108.                 }
  109.                 function d22()
  110.                 {
  111.                         $("div:empty").css("background-color","red");
  112.                 }
  113.                 function d23()
  114.                 {
  115.                         $("div:has(.mini)").css("background-color","red");
  116.                 }
  117.                 function d24()
  118.                 {
  119.                         $("div:parent").css("background-color","red");
  120.                 }
  121.                 function e25()
  122.                 {
  123.                         $("div[title]").css("background-color","red");
  124.                 }
  125.                 function e26()
  126.                 {
  127.                         $("div[title='test']").css("background-color","red");
  128.                 }
  129.                 function e27()
  130.                 {
  131.                         $("div[title!='test']").css("background-color","red");
  132.                 }
  133.                 function e28()
  134.                 {
  135.                         $("div[title^='te']").css("background-color","red");
  136.                 }
  137.                 function e29()
  138.                 {
  139.                         $("div[title$='est']").css("background-color","red");
  140.                 }
  141.                 function e30()
  142.                 {
  143.                         $("div[title*='es']").css("background-color","red");
  144.                 }
  145.                 function e31()
  146.                 {
  147.                         $("div[id][title*='es']").css("background-color","red");
  148.                 }
  149.                 function f32()
  150.                 {
  151.                         $("div[class='one'] :nth-child(2)").css("background-color","red");
  152.                 }
  153.                 function f33()
  154.                 {
  155.                         $("div[class='one'] :first-child").css("background-color","red");
  156.                 }
  157.                 function f34()
  158.                 {
  159.                         $("div[class='one'] :last-child").css("background-color","red");
  160.                 }
  161.                 function f35()
  162.                 {
  163.                         $("div[class='one'] :only-child").css("background-color","red");
  164.                 }
  165.     </scrip去掉t>

  166.    
  167.        
  168. </head>
  169. <body>
  170.         <input type="radio" name="rdo" id="rdo1" target="f1" /><label for="rdo1">基本选择器</label>
  171.         <input type="radio" name="rdo" id="rdo2" target="f2" /><label for="rdo2">层次选择器</label>
  172.         <input type="radio" name="rdo" id="rdo3" target="f3" /><label for="rdo3">基本过滤选择器</label>
  173.         <input type="radio" name="rdo" id="rdo4" target="f4" /><label for="rdo4">内容过滤选择器</label>
  174.         <input type="radio" name="rdo" id="rdo5" target="f5" /><label for="rdo5">属性选择器</label>
  175.         <input type="radio" name="rdo" id="rdo6" target="f6" /><label for="rdo6">子元素过滤选择器</label>
  176.         <!-- 控制按钮 -->
  177.         <fieldset id="f1">
  178.         <!--<legend>Title</legend>-->
  179.         <h3>基本选择器.</h3>
  180.         <input type="button" value="选择 id为 one 的元素." id="btn1" onclick="a1();" />  
  181.         <input type="button" value="选择 class 为 mini 的所有元素." id="btn2" onclick="a2();" />
  182.         <input type="button" value="选择 元素名是 div 的所有元素." id="btn3" onclick="a3();"  />
  183.         <input type="button" value="选择 所有的元素." id="btn4" onclick="a4();"  />
  184.         <input type="button" value="选择 所有的span元素和id为two的元素." id="btn5" onclick="a5();" />
  185.         </fieldset>

  186.         <fieldset id="f2">
  187.         <h3>层次选择器.</h3>
  188.         <input type="button" value="选择 body内的所有div元素." id="btn6" onclick="b6();" />
  189.         <input type="button" value="在body内,选择子元素是div的。" id="btn7" onclick="b7();"  />
  190.         <input type="button" value="选择 所有class为one 的下一个div元素." id="btn8" onclick="b8();"  />
  191.         <input type="button" value="选择 所有class为one 的下一个class不为one的div元素." id="btn8_a" onclick="b8_a();" />
  192.         <input type="button" value="选择 id为two的元素后面的所有div兄弟元素." id="btn9" onclick="b9();"  />
  193.         </fieldset>

  194.         <fieldset id="f3">
  195.         <h3>基本过滤选择器.</h3>
  196.         <input type="button" value="选择第一个div元素." id="btn10" onclick="c10();" />
  197.         <input type="button" value="选择最后一个div元素." id="btn11" onclick="c11();"/>
  198.         <input type="button" value="选择class不为one的 所有div元素." id="btn12" onclick="c12();"/>
  199.         <input type="button" value="选择索引值为偶数 的div元素." id="btn13" onclick="c13();"/>
  200.         <input type="button" value="选择索引值为奇数 的div元素." id="btn14" onclick="c14();"/>
  201.         <input type="button" value="选择索引值等于3的元素." id="btn15" onclick="c15();"/>
  202.         <input type="button" value="选择索引值大于3的元素." id="btn16" onclick="c16();"/>
  203.         <input type="button" value="选择索引值小于3的元素." id="btn17" onclick="c17();"/>
  204.         <input type="button" value="选择所有的标题元素." id="btn18" onclick="c18();"/>
  205.         </fieldset>

  206.         <fieldset id="f4">
  207.         <h3>内容过滤选择器.</h3>
  208.         <input type="button" value="选取含有文本“di”的div元素." id="btn21" onclick="d21();"/>
  209.         <input type="button" value="选取不包含子元素(或者文本元素)的div空元素." id="btn22" onclick="d22();"/>
  210.         <input type="button" value="选取含有class为mini元素 的div元素." id="btn23" onclick="d23();"/>
  211.         <input type="button" value="选取含有子元素(或者文本元素)的div元素." id="btn24" onclick="d24();"/>
  212.         </fieldset>

  213.         <fieldset id="f5">
  214.         <h3>属性选择器.</h3>
  215.         <input type="button" value="选取含有 属性title 的div元素." id="btn25" onclick="e25();"/>
  216.         <input type="button" value="选取 属性title值等于“test”的div元素." id="btn26" onclick="e26();"/>
  217.         <input type="button" value="选取 属性title值不等于“test”的div元素(没有属性title的也将被选中)." id="btn27" onclick="e27();"/>
  218.         <input type="button" value="选取 属性title值 以“te”开始 的div元素." id="btn28" onclick="e28();"/>
  219.         <input type="button" value="选取 属性title值 以“est”结束 的div元素." id="btn29" onclick="e29();"/>
  220.         <input type="button" value="选取 属性title值 含有“es”的div元素." id="btn30" onclick="e30();"/>
  221.         <input type="button" value="组合属性选择器,首先选取有属性id的div元素,然后在结果中 选取属性title值 含有“es”的元素." id="btn31" onclick="e31();"/>
  222.         </fieldset>

  223.         <fieldset id="f6">
  224.         <h3>子元素过滤选择器.</h3>
  225.         <input type="button" value="选取每个class为one的div父元素下的第2个子元素." id="btn32" onclick="f32();"/>
  226.         <input type="button" value="选取每个class为one的div父元素下的第一个子元素." id="btn33" onclick="f33();"/>
  227.         <input type="button" value="选取每个class为one的div父元素下的最后一个子元素." id="btn34" onclick="f34();"/>
  228.         <input type="button" value="如果class为one的div父元素下的仅仅只有一个子元素,那么选中这个子元素." id="btn35" onclick="f35();"/>
  229.         </fieldset>

  230.         <br /><br />

  231.         <div class="one" id="one" >
  232.                 div:id为one,class为one  
  233.                 <div class="mini">class为mini</div>
  234.         </div>

  235.         <div class="one"  id="two" title="test" >
  236.                 div:id为two,class为one,title为test  
  237.                 <div class="mini"  title="other">class为mini,title为other</div>
  238.                 <div class="mini"  title="test">class为mini,title为test</div>
  239.         </div>

  240.         <div class="one">
  241.                 class为one  
  242.                 <div class="mini">class为mini</div>
  243.                 <div class="mini">class为mini</div>
  244.                 <div class="mini">class为mini</div>
  245.                 <div class="mini"></div>
  246.         </div>

  247.         <div class="one">
  248.                 class为one  
  249.                 <div class="mini">class为mini</div>
  250.                 <div class="mini">class为mini</div>
  251.                 <div class="mini">class为mini</div>
  252.                 <div class="mini"  title="tesst">class为mini,title为tesst</div>
  253.         </div>

  254.         <div>div</div>

  255.         <div>
  256.                 包含input的type为"hidden"的div<input type="hidden" size="8"/>
  257.         </div>

  258.         <span id="sp1">我是span元素.</span>
  259. </body>
  260. </html>
Copy code
Demo.htm (9.12 KB, Number of downloads: 0)

Score

Number of participants1MB+1 contribute+1 Collapse reason
The temperature in the northern city is 22 degrees + 1 + 1 Like one!

See all ratings





Previous:Code for booting auto-start exe in .net?
Next:【Remove lol garbage plugin】LOL memory optimization junk cleaner v2.0
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com