Home > Web Front-end > JS Tutorial > body text

What are the jquery attribute filter selectors?

青灯夜游
Release: 2020-11-13 17:12:53
Original
3387 people have browsed it

jquery attribute filter selectors include: "[attribute]", "[attribute=value]", "[attribute*=value]", "[attribute~=value]", "[attribute!= value]", "[attribute^=value]", etc.

What are the jquery attribute filter selectors?

【Related recommendations: jQuery video tutorial

jquery attribute filter selector

In HTML documents, the start tag of an element usually contains multiple attributes. In jQuery, in addition to directly using the id and class attributes as selectors, you can also Filter the elements queried by the selector according to various attributes (such as title, etc.)

Attribute filtering selector is contained in square brackets "[]" instead of starting with a colon, usually using "select "[Attribute filter selector]" syntax format, you can filter the queried elements based on whether they contain specified attributes or based on attribute values.

1: Contains attribute filter "[attribute]"

Used to select all elements that contain a given attribute

Format:

$("selector[attribute]")
Copy after login

2: Attribute equal filter "[attribute=value]"

Used to filter all elements where a given attribute is equal to a specific value

     $("selector[attribute=value]");
Copy after login

Example :

$("input[name=accept]").attr("checked", "true");  //将name为accept的复选框选中
Copy after login

3: Attribute contains filter "[attribute *= value]"

Used to select all elements whose specified attribute value contains the given string

Format:

$("selector[attribute*=value]")
Copy after login

Example:

$("input[name*='news']").val("name中包含有news的元素");  //将name中包含'news'的文本框添加文本值
Copy after login

4: The attribute contains the word filter "[attribute ~= value]"

Use Select elements that contain the given word (separated by spaces) in the specified attribute value

Format:

$("selector[attribute~=value]");
Copy after login

Example:

$('input[name~='news']').val("name中包含news单词的元素");
Copy after login

5: Attribute is not equal to filter Tool "[attribute !=value]"

Used to select all elements that do not contain the specified attribute, or contain the specified attribute but the attribute is not equal to a value

Format:

$("selector[attribute!=value]")
Copy after login

6: Attribute start filter "[attribute ^= value]"

Used to select all elements where a given attribute starts with a specific value

Format:

$("selector[attribute^=value]")
Copy after login

7: Attribute end filter "[attribute $= value]"

Used to select whether a given attribute ends with a specific value All elements

Format:

$("selector[attribute$=value]")
Copy after login

8: Composite attribute filter

Used to select all elements that meet multiple conditions at the same time

Format:

$("selector[selector1][selector2]...[selector[N]")
Copy after login

Example:

$("input[id][name^='news']").val("复合条件")//用于选择包含有id属性并且name值以'news'开头的文本框,并对其值进行设置
Copy after login

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the jquery attribute filter selectors?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template