Home >Web Front-end >JS Tutorial >What are the jquery attribute filter selectors?
jquery attribute filter selectors include: "[attribute]", "[attribute=value]", "[attribute*=value]", "[attribute~=value]", "[attribute!= value]", "[attribute^=value]", etc.
【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]")
2: Attribute equal filter "[attribute=value]"
Used to filter all elements where a given attribute is equal to a specific value
$("selector[attribute=value]");
Example :
$("input[name=accept]").attr("checked", "true"); //将name为accept的复选框选中
3: Attribute contains filter "[attribute *= value]"
Used to select all elements whose specified attribute value contains the given string
Format:
$("selector[attribute*=value]")
Example:
$("input[name*='news']").val("name中包含有news的元素"); //将name中包含'news'的文本框添加文本值
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]");
Example:
$('input[name~='news']').val("name中包含news单词的元素");
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]")
6: Attribute start filter "[attribute ^= value]"
Used to select all elements where a given attribute starts with a specific value
Format:
$("selector[attribute^=value]")
7: Attribute end filter "[attribute $= value]"
Used to select whether a given attribute ends with a specific value All elements
Format:
$("selector[attribute$=value]")
8: Composite attribute filter
Used to select all elements that meet multiple conditions at the same time
Format:
$("selector[selector1][selector2]...[selector[N]")
Example:
$("input[id][name^='news']").val("复合条件")//用于选择包含有id属性并且name值以'news'开头的文本框,并对其值进行设置
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!