ドキュメント

Manipulation/insertBefore

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Manipulation

insertBefore( selector )

Insert all of the matched elements before another, specified, set of elements. As of jQuery 1.3.2, returns all of the inserted elements.

This operation is, essentially, the reverse of doing a regular $(A).before(B), in that instead of inserting B before A, you're inserting A before B.

The elements must already be inserted into the document (you can't insert an element after another if it's not in the page).

Arguments:
selectorString
Content after which the selected element(s) is inserted.
Examples:

Inserts all paragraphs before an element with id of "foo". Same as $("#foo").before("p")

$("p").insertBefore("#foo"); // check before() examples

<!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").insertBefore("#foo"); // check before() examples
  });
  </script>
  <style>#foo { background:yellow; }</style>
</head>
<body>
  <div id="foo">FOO!</div><p>I would like to say: </p>
</body>
</html>

NameType