JQuery选择器中的子元素选择器

<p>子元素筛选选择器不常使用,其筛选规则比起其它的选择器稍微要复杂点,其实博主感觉并不怎么难啦,因为单词so easy,哈哈。</p><p>我们来看看都有哪些吧:</p><p>这里写图片描述</p><p>注意:</p><p>1. :first只匹配一个单独的元素,但是:first-child选择器可以匹配多个:即为每个父级元素匹配第一个子元素。这相当于:nth-child(1)</p><p>2.:last 只匹配一个单独的元素, :last-child 选择器可以匹配多个元素:即,为每个父级元素匹配最后一个子元素</p><p>3.如果子元素只有一个的话,:first-child与:last-child是同一个</p><p>4. :only-child匹配某个元素是父元素中唯一的子元素,就是说当前子元素是父元素中唯一的元素,则匹配</p><p>5.jQuery实现:nth-child(n)是严格来自CSS规范,所以n值是&quot;索引&quot;,也就是说,从1开始计数-child(index)从1开始的,而开始的,而eq(index)是从0开始的</p><p>6.nth-child(n) 与 :nth-last-child(n) 的区别前者是从前往后计算,后者是从后往前记。</p><pre class="brush:html;toolbar:false">&lt;!DOCTYPEhtml&gt; &lt;html&gt; &lt;head&gt; &lt;metahttp-equiv=&quot;Content-type&quot;content=&quot;text/html;charset=utf-8&quot;/&gt; &lt;title&gt;&lt;/title&gt; &lt;linkrel=&quot;stylesheet&quot;href=&quot;imooc.css&quot;type=&quot;text/css&quot;&gt; &lt;scriptsrc=&quot;http://libs.baidu.com/jquery/1.9.1/jquery.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;子元素筛选选择器&lt;/h2&gt; &lt;h3&gt;:first-child、:last-child、:only-child&lt;/h3&gt; &lt;divclass=&quot;leftfirst-div&quot;&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;a&gt;第二个元素&lt;/a&gt; &lt;a&gt;:last-child&lt;/a&gt; &lt;/div&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;/div&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;a&gt;第二个元素&lt;/a&gt; &lt;a&gt;:last-child&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; //查找所有class=&quot;first-div&quot;下的a元素,只取第一个 //针对所有父级下的第一个 $(&quot;.first-diva:first-child&quot;).css(&quot;color&quot;,&quot;#CD00CD&quot;); &lt;/script&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; //查找所有class=&quot;first-div&quot;下的a元素,只取最后一个 //针对所有父级下的最后一个 //如果只有一个元素的话,last也是第一个元素 $(&quot;.first-diva:last-child&quot;).css(&quot;color&quot;,&quot;red&quot;); &lt;/script&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; //查找所有class=&quot;first-div&quot;下的a元素,如果只有一个子元素的情况 $(&quot;.first-diva:only-child&quot;).css(&quot;color&quot;,&quot;blue&quot;); &lt;/script&gt; &lt;h3&gt;:nth-child、:nth-last-child&lt;/h3&gt; &lt;divclass=&quot;leftlast-div&quot;&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;a&gt;第二个元素&lt;/a&gt; &lt;a&gt;第三个元素&lt;/a&gt; &lt;a&gt;:last-child&lt;/a&gt; &lt;/div&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;a&gt;第二个元素&lt;/a&gt; &lt;/div&gt; &lt;divclass=&quot;div&quot;&gt; &lt;a&gt;:first-child&lt;/a&gt; &lt;a&gt;第二个元素&lt;/a&gt; &lt;a&gt;第三个元素&lt;/a&gt; &lt;a&gt;:last-child&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; //查找所有class=&quot;last-div&quot;下的a元素,选择第二个 $(&quot;.last-diva:nth-child(2)&quot;).css(&quot;color&quot;,&quot;#CD00CD&quot;); &lt;/script&gt; &lt;scripttype=&quot;text/javascript&quot;&gt; //查找所有class=&quot;last-div&quot;下的a元素,选择第倒数第二个 $(&quot;.last-diva:nth-last-child(2)&quot;).css(&quot;color&quot;,&quot;red&quot;); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre>
RangeTime:0.006413s
RangeMem:207.58 KB
返回顶部 留言