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:

设计酷,jquery1.6 api中文手册