jquery選取器之屬性過濾選取器詳解

來源:互聯網
上載者:User

複製代碼 代碼如下:
<style type="text/css">
  /*高亮顯示*/
  .highlight{  
   background-color: gray
  }
 </style>

複製代碼 代碼如下:
<body>
   <div>
      <p>Hello</p>
   </div>
   <div id="test">ID為test的DIV</div>
   <input type="checkbox" id="s1" name="football" value="足球" />足球
   <input type="checkbox" name="volleyball" value="排球" />排球
   <input type="checkbox" id="s3" name="basketball" value="籃球" />籃球
   <input type="checkbox" id="s4" name="other" value="其他" />其他
  </body>

1. [attribute]用法
定義:匹配包含給定屬性的元素
複製代碼 代碼如下:
$("div[id]").addClass("highlight"); //尋找所有含有ID屬性的div元素

2. [attribute=value]用法
定義:匹配給定的屬性是某個特定值的元素
複製代碼 代碼如下:
$("input[name='basketball']").attr("checked",true);   //name屬性值為basketball的input元素選中

3. [attribute!=value]用法
定義:匹配給定的屬性是不包含某個特定值的元素
複製代碼 代碼如下:
$("input[name!='basketball']").attr("checked",true);   //name屬性值不為basketball的input元素選中
//此選取器等價於:not([attr=value])要匹配含有特定屬性但不等於特定值的元素,請使用[attr]:not([attr=value])
$("input:not(input[name='basketball'])").attr("checked",true);

4. [attribute^=value]用法
定義:匹配給定的屬性是以某些值開始的元素
複製代碼 代碼如下:
$("input[name^='foot']").attr("checked",true);  //尋找所有 name 以 'foot' 開始的 input 元素

5. [attribute$=value]用法
定義:匹配給定的屬性是以某些值結尾的元素
複製代碼 代碼如下:
$("input[name$='ball']").attr("checked",true); //尋找所有 name 以 'ball' 結尾的 input 元素

6. [attribute*=value]用法
定義:匹配給定的屬性是以包含某些值的元素
複製代碼 代碼如下:
$("input[name*='sket']").attr("checked",true);  //尋找所有 name 包含 'sket' 的 input 元素

7. [selector1][selector2][selectorN]用法
定義:複合屬性選取器,需要同時滿足多個條件時使用
複製代碼 代碼如下:
$("input[id][name$='ball']").attr("checked",true);  //找到所有含有 id屬性,並且它的 name屬性是以 ball結尾的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.