Attribute Starts With Selector [name^="value"]


attributeStartsWith selector

描述: 选择指定属性是以给定字符串开始的元素

  • 添加的版本: 1.0jQuery( "[attribute^='value']" )

    attribute: 一个属性名.

    value: 一个属性值,可以是一个不带引号的一个单词或带一个引号的字符串。

这个选择器能很方便的定位一些由服务器端框架生成的语义化的 ID,它们可能带有相同的前缀。然而这个选择器的速度要比用 class 选择器慢得多。所以如果可能的话,最好在这些元素上生成相同的 class,之后使用 class 选择器来选中它们。

例子:

Finds all inputs with an attribute name that starts with 'news' and puts text in them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input name="newsletter" />
<input name="milkman" />
<input name="newsboy" />
<script>$('input[name^="news"]').val('news here!');</script>
</body>
</html>

Demo: