Has Attribute Selector [name]


attributeHas selector

描述: 选择所有具有指定属性的元素,该属性可以是任何值。

  • 添加的版本: 1.0jQuery( "[attribute]" )

    attribute: 一个属性名

例子:

绑定一个单一的点击,增加了div的id的文本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>
<script>
$('div[id]').one('click', function(){
var idString = $(this).text() + ' = ' + $(this).attr('id');
$(this).text(idString);
});
</script>
</body>
</html>

Demo: