Has Attribute Selector [name]

attributeHas selector

version added: 1.0jQuery('[attribute]')

  • attribute
    一个属性名

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

Example:

Bind a single click that adds the div id to its text.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.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:

jQuery 1.6 API 中文版Clove整理、修订