Home php教程 PHP开发 Detailed explanation of attribute filter selector of jquery selector

Detailed explanation of attribute filter selector of jquery selector

Dec 17, 2016 pm 03:58 PM

<style type="text/css">
  /*高亮显示*/
  .highlight{   
   background-color: gray
  }
 </style>
Copy after login
<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>
Copy after login

[attribute] Usage
Definition: Match elements that contain the given attribute

$("div[id]").addClass("highlight"); //查找所有含有ID属性的div元素
Copy after login

2. [attribute=value] Usage
Definition: Match elements where the given attribute is a specific value

$("input[name=&#39;basketball&#39;]").attr("checked",true);   //name属性值为basketball的input元素选中
Copy after login

3. [attribute! =value] Usage
Definition: Match the given attribute is an element that does not contain a specific value

$("input[name!=&#39;basketball&#39;]").attr("checked",true);   //name属性值不为basketball的input元素选中 
//此选择器等价于:not([attr=value])要匹配含有特定属性但不等于特定值的元素,请使用[attr]:not([attr=value])
$("input:not(input[name=&#39;basketball&#39;])").attr("checked",true);
Copy after login

4. [attribute^=value] Usage
Definition: Match the given attribute is an element that starts with a certain value

$("input[name^=&#39;foot&#39;]").attr("checked",true);  //查找所有 name 以 &#39;foot&#39; 开始的 input 元素
Copy after login

5. [attribute$=value] Usage
Definition: Match the given attribute that ends with some value

$("input[name$=&#39;ball&#39;]").attr("checked",true); //查找所有 name 以 &#39;ball&#39; 结尾的 input 元素
Copy after login

6. [attribute*=value] Usage
Definition: Match the given attribute that ends with a certain value Elements of certain values

$("input[name*=&#39;sket&#39;]").attr("checked",true);  //查找所有 name 包含 &#39;sket&#39; 的 input 元素
Copy after login

7. [selector1][selector2][selectorN] Usage
Definition: Composite attribute selector, used when multiple conditions need to be met at the same time

$("input[id][name$=&#39;ball&#39;]").attr("checked",true);  //找到所有含有 id属性,并且它的 name属性是以 ball结尾的
Copy after login



More jquery selectors For detailed explanation of the attribute filter selector, please pay attention to the PHP Chinese website for related articles!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)