ドキュメント

Traversing/add

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Traversing

add( expr )

Adds more elements, matched by the given expression, to the set of matched elements.
Arguments:
exprString, DOMElement, Array<DOMElement>
An expression whose matched elements are added for String, a string of HTML to create on the fly for DOMElement or one or more Elements to add if an Array. A jQuery object is also accepted.

Examples:
Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.

    $("div").css("border", "2px solid red")
            .add("p")
            .css("background", "yellow");

<!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").css("border", "2px solid red")
            .add("p")
            .css("background", "yellow");

  });
  </script>
  <style>
  div { width:60px; height:60px; margin:10px; float:left; }
  p { clear:left; font-weight:bold; font-size:16px; 
      color:blue; margin:0 10px; padding:2px; }
  </style>
</head>
<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <p>Added this... (notice no border)</p>
</body>
</html>

Adds more elements, matched by the given expression, to the set of matched elements.

$("p").add("span").css("background", "yellow");

<!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(){
    $("p").add("span").css("background", "yellow");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><span>Hello Again</span>
</body>
</html>

Adds more elements, created on the fly, to the set of matched elements.

$("p").clone().add("<span>Again</span>").appendTo(document.body);

<!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(){
    $("p").clone().add("<span>Again</span>").appendTo(document.body);
  });
  </script>
  
</head>
<body>
  <p>Hello</p>
</body>
</html>

Adds one or more Elements to the set of matched elements.

$("p").add(document.getElementById("a")).css("background", "yellow");

<!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(){
    $("p").add(document.getElementById("a")).css("background", "yellow");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><span id="a">Hello Again</span>
</body>
</html>

Demonstrates how to add (or push) elements to an existing collection

var collection = $("p");
// capture the new collection
collection = collection.add(document.getElementById("a"));
collection.css("background", "yellow");

<!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(){
    var collection = $("p");
// capture the new collection
collection = collection.add(document.getElementById("a"));
collection.css("background", "yellow");
  });
  </script>
  
</head>
<body>
  <p>Hello</p><span id="a">Hello Again</span>
</body>
</html>

NameType