jQuery选择器大全(48个代码片段+21幅图演示)2

时间:2022-05-07
本文章向大家介绍jQuery选择器大全(48个代码片段+21幅图演示)2,主要内容包括4. 属性过滤选择器、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

4. 属性过滤选择器

——4.1 [attribute](取拥有attribute属性的元素)

下面的代码,最后一个a标签没有title属性,所以它仍然会带下划线

<script type="text/javascript">
        $(document).ready(function() {
            $('a[title]').css('text-decoration', 'none');
       });    </script>       
    <ul>
        <li><a href="#" title="DOM对象和jQuery对象" class="item">DOM对象和jQuery对象</a></li>
        <li><a href="#" title="jQuery选择器大全" class="item-selected">jQuery选择器大全</a></li>
        <li><a href="#" title="jQuery事件大全" class="item">jQuery事件大全</a></li>
        <li><a href="#" title="基于jQuery的插件开发" class="item">基于jQuery的插件开发</a></li>
        <li><a href="#" title="Wordpress & jQuery" class="item">Wordpress & jQuery</a></li>
        <li><a href="#" class="item">其他</a></li>
    </ul>

——4.2 [attribute = value]和[attribute != value](取attribute属性值等于value或不等于value的元素)

分别为class="item"和class!=item的a标签指定文字颜色

<script type="text/javascript">
       $(document).ready(function() {
           $('a[class=item]').css('color', '#FF99CC');
           $('a[class!=item]').css('color', '#FF6600');
       });</script>  

——4.3 [attribute ^= value], [attribute $= value]和[attribute *= value](attribute属性值以value开始,以value结束,或包含value值)

在属性选择器中,^$符号和正则表达式的开始结束符号表示的含义是一致的,*模糊匹配,类似于sql中的like '%str%'。

<script type="text/javascript">
    // 识别大小写,输入字符串时可以输入引号,[title^=jQuery]和[title^="jQuery"]是一样的
    $('a[title^=jQuery]').css('font-weight', 'bold');
    $('a[title$=jQuery]').css('font-size', '24px');
    $('a[title*=jQuery]').css('text-decoration', 'line-through');</script>

——4.4 [selector1][selector2](复合型属性过滤器,同时满足多个条件)

将title以"jQuery"开始,并且class="item"的a标签隐藏,那么<a href="#" title="jQuery事件大全" class="item">jQuery事件大全</a>会被隐藏

<script type="text/javascript">
        $(document).ready(function() {
            $('a[title^=jQuery][class=item]').hide();
        });    </script>  

5. 子元素过滤选择器

——5.1 :first-child和:last-child

:first-child表示第一个子元素,:last-child表示最后一个子元素。

需要大家注意的是,:fisrst和:last返回的都是单个元素,而:first-child和:last-child返回的都是集合元素。举个例子:div:first返回的是整个DOM文档中第一个div元素,而div:first-child是返回所有div元素下的第一个元素合并后的集合。

这里有个问题:如果一个元素没有子元素,:first-child和:last-child会返回null吗?请看下面的代码:

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var len1 = $('div:first-child').length;        var len2 = $('div:last-child').length;
     });    </script></head><body><div>
    <div>
        <div></div>
    </div></div></body></html>

也许你觉得这个答案,是不是太简单了?len1 = 2, len2 = 2。但实际确并不是,它们俩都等于3。 把上面的代码稍微修改一下:

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var len1 = $('div:first-child').length;        var len2 = $('div:last-child').length;
        $('div:first-child').each(function() {
            alert($(this).html());
        });
     });    </script></head><body><div>123    <div>456        <div></div>
    </div></div></body></html>

结果却是弹出三个alert,只不过最后一个alert里面是空白的。

——5.2 :only-child

当某个元素有且仅有一个子元素时,:only-child才会生效。

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('div:only-child').css('border', '1px solid #FF0000').css('width','200px');
        });    </script></head><body><div>123    <div>456        <div></div>
    </div></div></body></html>

这里:only-child也是三个元素,从最后一个很粗的红色边框(实际是两个元素的边框重叠了)也可以看出来。

——5.3 :nth-child

看到这个就想起英文单词里的,fourth, fifth, sixth……,nth表示第n个,:nth-child就表示第n个child元素。要注意的是,这儿的n不像eq(x)、gt(x)或lt(x)是从0开始的,它是从1开始的,英文里好像也没有zeroth这样的序号词吧。

:nth-child有三种用法:

1) :nth-child(x),获取第x个子元素 2) :nth-child(even)和:nth-child(odd),从1开始,获取第偶数个元素或第奇数个元素 3) :nth-child(xn+y),x>=0,y>=0。例如x = 3, y = 0时就是3n,表示取第3n个元素(n>=0)。实际上xn+y是上面两种的通项式。(当x=0,y>=0时,等同于:hth-child(x);当x=2,y=0时,等同于nth-child(even);当x=2,y=1时,等同于:nth-child(odd))

下面的两个例子是针对2)和3)的,1)的例子我就不列举了。

例2:

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">
    <title></title>
    <style type="text/css">
        
        td {            width: 200px;            height: 32px;            line-height: 32px;        }
        
    </style>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            // 偶数行背景红色
            $('tr:nth-child(even)').css('background', '#FF0000');            // 奇数行背景蓝色
            $('tr:nth-child(odd)').css('background', '#0000FF');
        });    </script></head><body>
    <table>
        <tr><td>1. NBA 2012季后赛</td></tr>
        <tr><td>2. NBA 2011季后赛</td></tr>
        <tr><td>3. NBA 2010季后赛</td></tr>
        <tr><td>4. NBA 2009季后赛</td></tr>
        <tr><td>5. NBA 2008季后赛</td></tr>
        <tr><td>6. NBA 2007季后赛</td></tr>
    </table></body></html>

例3(html代码和例2是一样的):

<script type="text/javascript">
    $(document).ready(function() {
        $('tr:nth-child(3n)').css('background', '#0000FF');
    });</script>

6. 表单对象属性过滤选择器

——6.1 :enabled和:disabled(取可用或不可用元素)

:enabled和:diabled的匹配范围包括input, select, textarea。

<script type="text/javascript">
        $(document).ready(function() {
            $(':enabled').css('border', '1px solid #FF0000');
            $(':disabled').css('border', '1px solid #0000FF');
        });    </script>
    <div>
        <input type="text" value="可用的文本框" />
    </div>
    <div>
        <input type="text" disabled="disabled" value="不可用的文本框" />
    </div>
    <div>
        <textarea disabled="disabled">不可用的文本域</textarea>
    </div>
    <div>
        <select disabled="disabled">
            <option>English</option>
            <option>简体中文</option>
        </select>
    </div>

——6.2 :checked(取选中的单选框或复选框元素)

下面的代码,更改边框或背景色仅在IE下有效果,chrome和firefox不会改变,但是alert都会弹出来。

<script type="text/javascript">
    $(document).ready(function() {
        $(':checked').css('background', '#FF0000').each(function() {
            alert($(this).val());
        });
    });</script><div>
    <input type="checkbox" checked="checked" value="must"/>必须勾选</div><div>你现在工作的企业属于:    <input type="radio" name="radio" checked="checked" value="外企"/>外企    <input type="radio" name="radio" value="国企"/>国企    <input type="radio" name="radio" value="民企"/>民企</div>

——6.3 :selected(取下拉列表被选中的元素)

<script type="text/javascript">
    $(document).ready(function() {
        alert($(':selected').val());
    });</script><select>
    <option value="外企">外企</option>
    <option value="国企">国企</option>
    <option value="私企">私企</option></select>

四、表单选择器

1. :input(取input,textarea,select,button元素)

:input元素这里就不再多说了,前面的一些例子中也已经囊括了。

2. :text(取单行文本框元素)和:password(取密码框元素)

这两个选择器分别和属性选择器$('input[type=text]')、$('input[type=password]')等同。

<script type="text/javascript">
   $(document).ready(function() {
        $(':text').css('border', '1px solid #FF0000');
        $(':password').css('border', '1px solid #0000FF');        // 等效代码
        //$('input[type=text]').css('border', '1px solid #FF0000');
        //$('input[type=password]').css('border', '1px solid #0000FF');
   });</script><fieldset style="width: 300px;">
    <legend>账户登录</legend>
     <div>
        <label>用户名:</label><input type="text"/>
    </div>
    <div>
        <label>密&nbsp;&nbsp;码:</label><input type="password"/>
    </div></fieldset>

3. :radio(取单选框元素)

:radio选择器和属性选择器$('input[type=radio]')等同

<script type="text/javascript">
    $(document).ready(function() {
        $(':radio').each(function() {
            alert($(this).val());
        });        // 等效代码
        /*
        $('input[type=radio]').each(function() {
            alert($(this).val());
        });
        */
    });</script>你现在工作的企业属于:    <input type="radio" name="radio" checked="checked" value="外企"/>外企    <input type="radio" name="radio" value="国企"/>国企    <input type="radio" name="radio" value="民企"/>民企

4. :checkbox(取复选框元素)

:checkbox选择器和属性选择器$('input[type=checkbox]')等同

<script type="text/javascript">
    $(document).ready(function() {
        $(':checkbox').each(function() {
            alert($(this).val());
        });        // 等效代码
        /*
        $('input[type=checkbox]').each(function() {
            alert($(this).val());
        });
        */
    });</script>
    您的兴趣爱好:    <input type="checkbox" />游泳    <input type="checkbox" />看书    <input type="checkbox" checked="checked" value="打篮球"/>打篮球    <input type="checkbox" checked="checked" value="电脑游戏"/>电脑游戏

上面的代码,会将所有额checkbox的value输出出来。若你想选择选中项,有三种写法:

$(':checkbox:checked').each(function() {
    alert($(this).val());
});

$('input[type=checkbox][checked]').each(function() {
    alert($(this).val());
});

$(':checked').each(function() {
    alert($(this).val());
});

5. :submit(取提交按钮元素)

:submit选择器和属性选择器$('input[type=submit]')等同

6. :reset(取重置按钮元素)

:reset选择器和属性选择器$('input[type=reset]')等同

7. :button(取按钮元素)

:button选择器和属性选择器$('input[type=button]')等同

8. :file(取上传域元素)

:file选择器和属性选择器$('input[type=file]')等同

9. :hidden(取不可见元素)

:hidden选择器和属性选择器$('input[type=hidden]')等同

表单选择器的6~10例子我就不再列举了,和1~5的很类似,大家可以自己尝试一下。