Home >Web Front-end >JS Tutorial >How to use jQuery attribute filter selector
This article mainly brings you a detailed explanation of the usage of the attribute filter selector of jQuery selector. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Among so many attribute selectors, [attr="value"] and [attr*="value"] are the most practical The
[attr="value"] can help us locate different types of elements, especially the operation of form elements, such as input[type="text"], input[ type="checkbox"], etc.
[attr*="value"] can help us match different types of files on the website
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" href="imooc.css" rel="external nofollow" type="text/css"> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> <h2>属性筛选选择器</h2> <h3>[att=val]、[att]、[att|=val]、[att~=val]</h3> <p class="left" testattr="true" > <p class="p" testattr="true" name='p1'> <a>[att=val]</a> </p> <p class="p" testattr="true" p2> <a>[att]</a> </p> <p class="p" testattr="true" name="-"> <a>[att|=val]</a> </p> <p class="p" testattr="true" name="a b"> <a>[att~=val]</a> </p> </p> <script type="text/javascript"> //查找所有p中,属性name=p1的p元素 $("p[name = p1]").css("border", "3px groove red"); </script> <script type="text/javascript"> //查找所有p中,有属性p2的p元素 $("p[p2]").css("border", "3px groove blue"); </script> <script type="text/javascript"> //查找所有p中,有属性name中的值只包含一个连字符“-”的p元素 $("p[name|='-']").css("border", "3px groove #00FF00"); </script> <script type="text/javascript"> //查找所有p中,有属性name中的值包含一个连字符“空”和“a”的p元素 $("p[name~='a']").css("border", "3px groove #668B8B"); </script> <h3>[att^=val]、[att*=val]、[att$=val]、[att!=val]</h3> <p class="left" testattr="true" > <p class="p" testattr="true" name='imooc-aaorn'> <a>[att^=val]</a> </p> <p class="p" testattr="true" name='aaorn-imooc'> <a>[att$=val]</a> </p> <p class="p" testattr="true" name="attr-test-selector"> <a>[att*=val]</a> </p> <p class="p" name="a b"> <a>[att!=val]</a> </p> </p> <script type="text/javascript"> //查找所有p中,属性name的值是用imooc开头的 $("p[name ^= imooc]").css("border", "3px groove red"); </script> <script type="text/javascript"> //查找所有p中,属性name的值是用imooc结尾的 $("p[name $= imooc]").css("border", "3px groove blue"); </script> <script type="text/javascript"> //查找所有p中,有属性name中的值包含一个test字符串的p元素 $("p[name*='test']").css("border", "3px groove #00FF00"); </script> <script type="text/javascript"> //查找所有p中,有属性testattr中的值没有包含"true"的p $("p[testattr != 'true']").css("border", "3px groove #668B8B"); </script> </body> </html>
Related recommendations:
Detailed examples of jQuery form object attribute filtering selector
10 course recommendations on attribute filtering
jQuery form element selector and getting select element selector instance
The above is the detailed content of How to use jQuery attribute filter selector. For more information, please follow other related articles on the PHP Chinese website!