jQuery中选择器(包含实例)

因为我查了一下,发现网上别人总结的确实也都不错了,所以我下面主要是对选择器的使用(其实主要还是为了督促自己多练习使用啦)

推荐两个我觉得不错的关于选择器的总结:jQuery选择器W3C-jQuery选择器

jQuery中的选择器,主要也就分为:基本选择器、属性选择器、内容选择器、过滤选择器、子元素选择器、表单元素选择器等

基本选择器,也是我们用的最多的,我就不再举例了

属性选择器的使用:

[attribute] $("[href]") 所有带有 href 属性的元素
[attribute=value] $("[href='#']") 所有 href 属性的值等于 "#" 的元素
[attribute!=value] $("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 所有 href 属性的值包含以 ".jpg" 结尾的元素

使用:

HTML:

<div id="wrap">
    <img src="images/1.jpg"/>
    <img src="images/2.jpg"/>
    <img src="images/3.jpg"/>
    <img src="images/4.jpg"/>
    <img src="images/ingoBg.png"/>
</div>
jQuery:
    // $("[src$='.png']").width(200);
    //  $("[src]").width(200);
    //  $("[src='images/1.jpg']").width(200);
    //  $("[src!='images/1.jpg']").width(200);

注意:如果里面和外面的引号不要使用一样的,尽量外面的使用双引号,里面的使用单引号

内容选择器和可视化选择器的使用:

:contains(text) $(":contains('W3School')") 包含指定字符串的所有元素
:empty $(":empty") 无子(元素)节点的所有元素
:hidden $("p:hidden") 所有隐藏的 <p> 元素
:visible $("table:visible") 所有可见的表格
使用:
    $(":contains('leo')").css('color','red')
    $(':empty').remove();
    $('h1:hidden').css('display','block');
    $('table:visible').css('background','#eee');
基本过滤选择器:
: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   所有动画元素
使用举例:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       ul{list-style:none;}
       h1{width:300px; height:40px; border:1px solid #0e566c; transform:translate(40px);}
        li{width:200px; height:50px; line-height:30px; text-align:center;border:1px solid #e15671;}
    </style>
    <script src="./js/src/jquery-1.8.3.js"></script>
</head>
<body>
<div id="wrap">
    <h1>列表</h1>
   <ul>
       <li>one</li>
       <li>two</li>
       <li>three</li>
       <li>four</li>
       <li>five</li>
       <li>six</li>
       <li>seven</li>
   </ul>
</div>
<script>
$('li:first').width(300);
$('li:last').width(300);
$('li:even').height(30);
$('li:odd').height(80);
$('li:eq(4)').css('background','yellow');
$('li:gt(4)').css('background','red');
$('li:lt(4)').css('background','blue');
$('li:not(:contains("five"))').css('color','#fff');
$(':header').height(100);
$(':animated').css('background','yellow');
</script>
</body>
</html>

表单选择器:这个其实就很简单了,一个会了,其他的都一样

: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 元素
$(':input').width(200);
$(':button').width(80);
$(':text').width(200);
其实选择器本来也挺简单,最重要的就是要多使用多练习,自然就掌握了


猜你喜欢

转载自blog.csdn.net/lhjuejiang/article/details/80751311