现在的位置: 首页 > 综合 > 正文

过滤选择器

2018年05月06日 ⁄ 综合 ⁄ 共 3409字 ⁄ 字号 评论关闭

1、基本过滤选择器
此项选择器搭配基本选择器可以讲占到实际选择器应用的90%以上(可由document.getElementById及节点nodeChilds得知)

:first//(选取第一个元素)
:last//(选取最后一个元素)
:even//(选取索引是偶数的所有元素)
:odd//(选取索引是奇数的所有元素)
:eq(index)//(选取索引等于index的元素)
:gt(index)//(选取索引大于index的元素)
:lt(index)//(选取索引小于index的元素)
:header//(选取所有的h1,h2,h3等标题元素)
:animated//(选取当前正在执行动画的所有元素)

2、内容过滤选择器

:contains(text)//选取含有文本内容为text的元素
:empty//选取不包含子元素或者文本的空元素
:has(selector)//选取含有选择器所有匹配的元素的元素
:parent//选取含有子元素或者文本的元素

3、可见性过滤选择器
对于<input type="hidden" />不要考虑对其应用任何css属性。That's no way。

:hidden//选取所有不可见的元素(包括<input type="hidden" />、<div style="display:none">和<div style="visibility:hidden;">;若只选取<input type="hidden" />使用$("input:hidden")
:visible//选取所有可见元素

4、属性选择过滤器
在ie6中对css的属性过滤选择器不起作用,而Jquery的属性选择过滤器经测试后在ie6中有效。(6.0之前不起作用)想要了解^ $ *可参考相关正则表达式资料。

[attribute]//选取拥有此属性的元素
[attribute=value]//选取属性的值为value的元素
[attribute!=value]//选取属性的值不为value的元素
[attribute^=value]//选取属性的值以value开始的元素
[attribute$=value]//选取属性的值以value结束的元素
[attribute*=value]//选取属性的含有value的元素
[selector1][selector2][selectorN]//属性选择器合并成一个复合属性选择器,注意此处为属性选择器的并集,如$("div[id][class$='Bar']"

5、子元素选择过滤器
同上面的属性选择器一样,原先的css规则对ie6不起作用,经过jquery可以选定该类元素。

:nth-child(index/even/odd/equation)//选取第index个子元素或者奇偶元素
:first-child//选取每个父元素的第一个元素(返回整个文档中每个元素的第一个子元素),如$("ul li:first-child");选择每个<ul>中第1个元素
:last-child//选取每个父元素的最后一个元素
:only-child//若某子元素是其父元素中惟一的子元素,将会被匹配

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

:enabled//选择所有可用元素,例$("#form1:enabled")
:disabled//选取所有不可用元素
:checked//选取所有被选中元素(checkbox,radio)
:selected//选取所有被选中元素(下拉列表)

7、表单选择器
不再赘述

:input
:text
:password
:radio
:checkbox
:submit
:image
:reset
:button
:file
:hidden

以下实例综合讲解了几个选择器的使用,同时演示了使用jquery实现隔行换色、荧光棒特效、复选框checkbox全选反选效果

<script type="text/javascript">
$(function(){
    $("table tr:even").addClass("tdOdd");
    $("th:first").css("background","#B4C6C1");//首个
    $("table tr").mouseover(function(){
        $(this).addClass("tdOver");}).mouseout(function(){
        $(this).removeClass("tdOver");}).click(function(){//荧光棒
        $(".tdClick").removeClass("tdClick");$(this).addClass("tdClick");
    })//行锁定
    $("input:checkbox:first").click(function(){
        $("input:checkbox:not(input:checkbox:first)").each(function(){//剔除本身
            $(this).attr("checked",$("input:checkbox:first").attr("checked"));
        })
    })
    $("input:checkbox:not(input:checkbox:first)").click(function(){
        var flag=true;
        $("input:checkbox:not(input:checkbox:first)").each(function(){
            if(!this.checked){flag=false;}//不可使用if($(this).attr("checked","false")){flag=false;}

        });
        $("input:checkbox:first").attr("checked",flag);
    })
});
</script>
<style type="text/css">
body{
font-size:12px;
color:#366;
}
table{
border:none;
background:#fefefe;
width:100%;
border-collapse:collapse;
}
th{
background:#CFDEC6;
padding:4px;
color:#000;
}
td,.tdNormal{
border:#cfdec6 solid 1px;
padding:4px;
background:fefefe;
}
.tdOdd{
background:#f1fefa;
}
.tdOver{
background:#F5FAF7;
}
</style>
</head>

<body>
<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>专业</th>
    </tr>
    <tr>
        <td>王洪剑</td>
        <td>22</td>
        <td>电气自动化</td>
    </tr>
    <tr>
        <td>李川川</td>
        <td>20</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>陈超</td>
        <td>22</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>秦玉龙</td>
        <td>21</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>刘威</td>
        <td>21</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>张会会</td>
        <td>21</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>胡海生</td>
        <td>30</td>
        <td>计算机</td>
    </tr>
    <tr>
        <td>吴雄</td>
        <td>22</td>
        <td>计算机</td>
    </tr>   
</table>

抱歉!评论已关闭.