侦测元素属性选择器 [name]
attributeHas selector
描述:选择具有指定的元素属性的元素,该元素属性可以是任意值。
加入于: 1.0
jQuery( "[attribute]" )attribute: 一个元素属性名。
示例
把一个点击事件绑定到一个带id的div,并把id添加到div的文本中。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attributeHas demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.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>
// Using .one() so the handler is executed at most once
// per element per event type
$( "div[id]" ).one( "click", function() {
var idString = $( this ).text() + " = " + $( this ).attr( "id" );
$( this ).text( idString );
});
</script>
</body>
</html>
演示结果