ドキュメント

Selectors/attributeHas

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Selectors

[attribute]

Matches elements that have the specified attribute. Note the "@" before the attribute name was deprecated as of version 1.2.
Arguments:
attributeString
An attribute name.
Examples:

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

    $("div[id]").one("click", function(){
      var idString = $(this).text() + " = " + $(this).attr("id");
      $(this).text(idString);
    });

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("div[id]").one("click", function(){
      var idString = $(this).text() + " = " + $(this).attr("id");
      $(this).text(idString);
    });

  });
  </script>
  
</head>
<body>
  <div>no id</div>
  <div id="hey">with id</div>
  <div id="there">has an id</div>
  <div>nope</div>
</body>
</html>

NameType