ドキュメント

Selectors/id

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Selectors

#id

Matches a single element with the given id attribute.
If the id contains characters like periods or colons you have to escape those characters with backslashes [1].
Arguments:
idString
An ID to search for, specified via the id attribute of an element.
Examples:

Finds the element with the id "myDiv".

$("#myDiv").css("border","3px solid red");

<!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(){
    $("#myDiv").css("border","3px solid red");
  });
  </script>
  <style>
  div {
    width: 90px;
    height: 90px;
    float:left;
    padding: 5px;
    margin: 5px;
    background-color: #EEEEEE;
  }
  </style>
</head>
<body>
  <div id="notMe"><p>id="notMe"</p></div>
  <div id="myDiv">id="myDiv"</div>
</body>
</html>

Finds the element with the id "myID.entry[1]". See how certain characters must be escaped with backslashes.

$("#myID\\.entry\\[1\\]").css("border","3px solid red");

<!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(){
    $("#myID\\.entry\\[1\\]").css("border","3px solid red");
  });
  </script>
  <style>
  div {
    width: 300px;
    float:left;
    padding: 2px;
    margin: 3px;
    background-color: #EEEEEE;
  }
  </style>
</head>
<body>
  <div id="myID.entry[0]">id="myID.entry[0]"</div>
  <div id="myID.entry[1]">id="myID.entry[1]"</div>
  <div id="myID.entry[2]">id="myID.entry[2]"</div>
</body>
</html>

NameType