概述 attributeStartsWith selector
返回值:Array<Element(s)>
描述:选择指定属性是以给定字符串开始的元素
attribute: 一个属性名.
value: 一个属性值,可以是一个 有效标识符或带一个引号的字符串。
这个选择器能很方便的定位一些由服务器端框架生成的语义化的 ID,它们可能带有相同的前缀。然而这个选择器的速度要比用 class 选择器慢得多。所以如果可能的话,最好在这些元素上生成相同的 class,之后使用 class 选择器来选中它们。
示例
查找所有 name 以 'news' 开始的 input 元素
<input name="newsletter" />
<input name="milkman" />
<input name="newsboy" />
$("input[name^='news']")
[ <input name="newsletter" />, <input name="newsboy" /> ]
Finds all inputs with an attribute name that starts with 'news' and puts text in them.
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>attributeStartsWith demo</title>
- <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
- </head>
- <body>
- <input name="newsletter">
- <input name="milkman">
- <input name="newsboy">
- <script>
- $( "input[name^='news']" ).val( "news here!" );
- </script>
- </body>
- </html>