jQuery选择器的总结

jQuery选择器

jQuery选择器是准确地选取你希望应用的元素。

jQuery的元素选择器和属性选择器允许你通过标签名、属性名或者内容对HTML元素的选择。

jQuery元素选择器表

选择器

实例

选取

*

$("*")

所有元素

#id

$("#lastname")

id="lastname" 的元素

.class

$(".intro")

所有 class="intro" 的元素

element

$("p")

所有 <p> 元素

.class.class

$(".intro.demo")

所有 class="intro" 且 class="demo" 的元素

 

 

 

:first

$("p:first")

第一个 <p> 元素

:last

$("p:last")

最后一个 <p> 元素

:even

$("tr:even")

所有偶数 <tr> 元素

:odd

$("tr:odd")

所有奇数 <tr> 元素

 

 

 

:eq(index)

$("ul li:eq(3)")

列表中的第四个元素(index 从 0 开始)

:gt(no)

$("ul li:gt(3)")

列出 index 大于 3 的元素

:lt(no)

$("ul li:lt(3)")

列出 index 小于 3 的元素

:not(selector)

$("input:not(:empty)")

所有不为空的 input 元素

 

 

 

:header

$(":header")

所有标题元素 <h1> - <h6>

:animated

 

所有动画元素

 

 

 

:contains(text)

$(":contains('W3School')")

包含指定字符串的所有元素

:empty

$(":empty")

无子(元素)节点的所有元素

:hidden

$("p:hidden")

所有隐藏的 <p> 元素

:visible

$("table:visible")

所有可见的表格

 

 

 

s1,s2,s3

$("th,td,.intro")

所有带有匹配选择的元素

 

 

 

[attribute]

$("[href]")

所有带有 href 属性的元素

[attribute=value]

$("[href='#']")

所有 href 属性的值等于 "#" 的元素

[attribute!=value]

$("[href!='#']")

所有 href 属性的值不等于 "#" 的元素

[attribute$=value]

$("[href$='.jpg']")

所有 href 属性的值包含以 ".jpg" 结尾的元素

 

 

 

:input

$(":input")

所有 <input> 元素

:text

$(":text")

所有 type="text" 的 <input> 元素

:password

$(":password")

所有 type="password" 的 <input> 元素

:radio

$(":radio")

所有 type="radio" 的 <input> 元素

:checkbox

$(":checkbox")

所有 type="checkbox" 的 <input> 元素

:submit

$(":submit")

所有 type="submit" 的 <input> 元素

:reset

$(":reset")

所有 type="reset" 的 <input> 元素

:button

$(":button")

所有 type="button" 的 <input> 元素

:image

$(":image")

所有 type="image" 的 <input> 元素

:file

$(":file")

所有 type="file" 的 <input> 元素

 

 

 

:enabled

$(":enabled")

所有激活的 input 元素

:disabled

$(":disabled")

所有禁用的 input 元素

:selected

$(":selected")

所有被选取的 input 元素

:checked

$(":checked")

所有被选中的 input 元素

 

jQuery基本选择器:

$(document).ready(function(){

         id选择器:指定的ID名

$('#div1').css('background', '#000');

class选择器:遍历类

$('.div1').css('background', '#000');

element选择器:遍历html元素

$('div').css('background', '#000');

*选择器:遍历所有元素

$('*').css('background', '#000');

并列选择器:

$(' p,div').css('background', '#000');

})

jQuery层次选择器:

$(document).ready(function(){

         “>”直系子元素

$('div>p').css('background', '#000');

“+”下一个兄弟元素等同于div

$('div+table').css('background', '#000');

“~”div的所有兄弟元素,等同于nextAll()方法

$('div~p').css('background', '#000');

 

})

jQuery过滤选择器:

$(document).ready(function(){

一:基本过滤选择器

         :first和last获取第一个元素或最后一个元素

$('li:first').css('background', '#000');

$('li:last').css('background', '#000');

:not获取非元素

$('li:not').css('background', '#000');

 :even和odd获取偶数索引或奇数索引元素,索引从零开始,even表示偶数,odd表示奇数

$('li:even').css('background', '#000');

$('li:odd').css('background', '#000');

 :eq(x)获取指定索引元素

$('li:eq(2)').css('background', '#000');

:gt(x) 和:lt(x)获取大于x索引或小于想索引的元素

$('li:gt(2)').css('background', '#000');

$('li:lt(3)').css('background', '#000');

:header获取h1~h6标题元素

$('li:header').css('background', '#000');

二内容过滤选择器

:contains(text)获取包含text文本的元素

$('li: contains(text) ').css('background', '#000');

:empty获取不包含子元素或文本为空的元素

$('li: empty ').css('background', '#000');

:has(selector)获取选择器匹配的元素

$('li: has(selector) ').css('background', '#000');

:parent获取包含子元素或文本的元素

$('ul li:parent ').css('background', '#000');

三可见性过滤选择器

:hidden获取不可见的元素

//jQuery至1.3.2之后的:hidden选择器仅匹配display:none或<input  type=”hidden”/>元素,而不匹配visibility:hidden或opacity:0的元素

         :visible获取可见元素

})

 

案例借鉴

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .SubCategoryBox{
            width:800px;
            border:1px solid #ccc;
            overflow: hidden;
            font-size: 16px;
        }
        .SubCategoryBox li{
            float: left;
            width: 240px;
            list-style: none;
        }
        .showmore{
            clear: both;
            text-align: center;
            padding: 5px 0 5px;
        }
        .showmore a{
            display: inline-block;
            border:1px solid green;
            padding:5px;
 
        }
        .showmore a:hover{
            background-color: light-green;
        }
</style>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
 
</head>
<body>
    <div class="SubCategoryBox">
        <ul>
            <li><a href="#">佳能</a><i>(30440)</i></li>
            <li><a href="#">索尼</a><i>(27220)</i></li>
            <li><a href="#">三星</a><i>(20808)</i></li>
            <li><a href="#">尼康</a><i>(17821)</i></li>
            <li><a href="#">松下</a><i>(12289)</i></li>
            <li><a href="#">卡西欧</a><i>(8242)</i></li>
            <li><a href="#">富士</a><i>(14894)</i></li>
            <li><a href="#">柯达</a><i>(9520)</i></li>
            <li><a href="#">宾得</a><i>(2195)</i></li>
            <li><a href="#">理光</a><i>(4114)</i></li>
            <li><a href="#">奥林巴斯</a><i>(12205)</i></li>
            <li><a href="#">明基</a><i>(1466)</i></li>
            <li><a href="#">爱国者</a><i>(3091)</i></li>
            <li><a href="#">其他品牌相机</a><i>(7275)</i></li>
        </ul>
        <div class="showmore">
            <a href="more.html"><span>显示全部品牌</span></a>
        </div>
    </div>
</body>
<script>
    $(function() { //等待DOM加载完毕
        var $category = $(".SubCategoryBox li:gt(4):not(:last)"); //获得索引值大于4的品牌集合对象(除最后一条)
        $category.hide(); //隐藏匹配元素
        var $toggleBtn = $("div.showmore>a"); //获取“显示全部品牌”按钮
        $toggleBtn.toggle(function() {
                //元素显示
                $category.show(); //显示$category
                $(this).find("span").text("精简显示品牌"); //改变按钮文字
                $(".SubCategoryBox li")
                    .filter(":contains('佳能'),:contains('尼康'),:contains('奥林巴斯')")
                    .addClass("promoted"); //推荐品牌高亮显示
                return false; //超链接不跳转
            },
            function() {
                //元素隐藏
                $category.hide(); //隐藏匹配元素
                $(this).find("span").text("显示全部品牌"); //改变按钮文字
                $(".SubCategoryBox li").removeClass("promoted"); //全部元素去掉"promoted"
                return false; //超链接不跳转
            });
    });
    </script>
 
</html>

 

posted @ 2016-09-13 21:03  岁月成风  阅读(173)  评论(0编辑  收藏  举报