jQuery的选择器及过滤器

 基本过滤选择器:

         :even和 :odd :匹配结果集中顺序为偶数(:even)或奇数(:odd)的元素。

         :header:匹配标题元素等标签

         :not : 不匹配后面选择器的元素

        :eq(index):匹配顺序号等于index的元素

        :gt(index):匹配顺序号大于index的元素

        :lt(index):匹配顺序号小于index的元素

         :firstfirst():获取第一个元素

        :lastlast():获取最后一个元素

        :first-child:last-child:only-childnth-child(index):分别匹配第一个、最后一个、“独生子”元素、第index个元素(这里的index1开始计数)

        :has(p):匹配包含p元素的元素

        :contains(‘this is my text’):匹配包含this is my text文本的元素

        :empty :匹配无内容的元素

        :parent :匹配拥有子元素的元素

        :hidden :匹配不可见的元素

        :visible :匹配可见的元素

        :animated :匹配正处于动画过程中的元素

        属性过滤选择器:

        [attribute]:获取包含给定属性的元素

        [attribute = value]:获取等于给定的属性是某个特定值的元素

        [attribute!=value]:获取不等于给定的属性石某个特定值得元素

        [attribute^=value]:获取给定属性是以某些值开始的元素

        [attribute$=value]:获取给定属性是以某些值结束的元素

        [attribute*=value]:获取给定属性是以包含某些值得元素

        [attribute~=value]:目标属性的值包含value中的任意一个子串(value由多个子串组成,子串之间由空格分隔)

        [attribute |=value]:目标属性的值等于value或者以value开头且其后紧跟一个连字符(-

        表单对象属性过滤选择器:

        :enabled:获取表单中所有属性为可用的元素

        :disabled:获取表单中所有属性为不可用的元素

        :checked:获取表单中所有被选中的元素

        :selected:获取表单中所有被选中的option的元素

        表单选择器:

        :input:获取所有inputtextareaselect

        :text:获取所有单行文本框

        :password:获取所有密码框

        :radio:获取所有单选按钮

        :checkbox:获取所有复选框

        :submit:获取所有提交按钮

        :image:获取所有图像域

        :reset:获取所有重置按钮

        :button:获取所有按钮

        :file:获取所有文件域

        过滤器的使用:

        利用:even和 :odd 生成条纹表格:

<!doctype html>

<html>

  <head>

  <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>

  <script>

  $(document).ready(function() {

    $('tr:even').css('background','#dedede');

    $('tr:odd').css('background','#ffffff');

  });

  </script>

  <body>

    <table>

      <tr>

      <th>Product</th>

      <th>Description</th>

      <th>Price</th>

      </tr>

      <tr>

      <td>Paper Towels</td>

      <td>The most absorbent paper towels.</td>

      <td>$18.99</td>

      </tr>

      <tr>

      <td>Paper Napkins</td>

      <td>Perfect for your outdoor gathering.</td>

      <td>$16.99</td>

      </tr>

      <tr>

      <td>Paper Plates</td>

      <td>The best value.</td>

      <td>$5.99</td>

      </tr>

      <tr>

      <td>Plastic Forks</td>

      <td>The essential picnic accessory.</td>

      <td>$2.99</td>

      </tr>

    </table>

  </body>

</html>

表格如图:


为列表或集合中的第一个或最后一个元素设置样式:

<!doctype html>

<html>

  <head>

  <style>

  ul {width:200px;font-family:arial;}

  ul li {border-bottom:1px solid #333;}

  ul li a {text-decoration:none;}

  </style>

  <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>

  <script>

  $(document).ready(function() {

    $('ul li:first').css('border','none');

    $('ul li:last').css('border','none');

  });

  </script>

  <body>

    <div id='sidebar'>

    <h1>My sidebar</h1>

    <ul>

      <li><a href='/index'>Home</a></li>

      <li><a href='/about'>About Us</a></li>

      <li><a href='/customer-service'>Customer Service</a></li>

      <li><a href='/contact'>Contact Us</a></li>

      <li><a href='/coupons'>Coupons</a></li>

    </ul>

    </div>

  </body>

</html>

页面效果如图:



        找出包含特定元素的元素:

        有时候,我们需要找出那些包含某个特定元素的元素。在这种情况下,:has过滤器就派上了用场。:has并不要求被包含的元素师父元素的直接子元素,只要是后代元素就行。

下面的这个例子使用:has过滤器找出具有content类并且包含p标签的元素,把他的字号设置成18像素:代码如下:

<!doctype html>

<html>

  <head>

  <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>

  <script>

  $(document).ready(function() {

    $('.content:has(p)').css('font-size','18px');

  });

  </script>

  <body>

    <div id='main'>

      <div class='content'>

      <p>This is my content</p>

      </div>

      <div class='alternate'>

      <p>This is alternate content.</p>

      </div>

    </div>

  </body>

</html>

浏览器中的效果如图:


        根据元素包含的文本过滤元素:

        有时候需要根据元素包含的内容匹配元素,这时可以用:contains过滤器实现这一目标。传递给:contains过滤器的文本可以用引号括起来,也可以不用。

       下面的例子使用:contains过滤器找出表格中包含Paper  Towels字样的单元格,为其添加1像素的虚线边框。代码如下:

<!doctype html>

<html>

  <head>

  <script src='jquery-1.9.1.min.js'></script>

  <script>

  $(document).ready(function() {

    $("tr td:contains('Paper Towels')").css('border','2px dotted red');

  });

  </script>

  <body>

    <table>

      <tr>

      <th>Product</th>

      <th>Description</th>

      <th>Price</th>

      </tr>

      <tr>

      <td>Paper Towels</td>

      <td>The most absorbent paper towels.</td>

      <td>$18.99</td>

      </tr>

      <tr>

      <td>Paper Napkins</th>

      <td>Perfect for your outdoor gathering.</th>

      <td>$16.99</th>

      </tr>

      <tr>

      <td>Paper Plates</td>

      <td>The best value.</td>

      <td>$5.99</td>

      </tr>

      <tr>

      <td>Plastic Forks</td>

      <td>The essential picnic accessory.</td>

      <td>$2.99</td>

      </tr>

    </table>

  </body>

</html>

浏览器中的效果如图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值