ドキュメント

Manipulation/remove

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Manipulation

remove[expr] )

Removes all matched elements from the DOM.
This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data. So:

$("#foo").remove().appendTo("#bar");

should be written as

$("#foo").appendTo("#bar");

to avoid losing the event handlers.

Can be filtered with an optional expression.
Arguments:
expr (Optional)String
A jQuery expression to filter the set of elements to be removed.
Examples:

Removes all paragraphs from the DOM

    $("button").click(function () {
      $("p").remove();
    });

<!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(){
    
    $("button").click(function () {
      $("p").remove();
    });

  });
  </script>
  <style>p { background:yellow; margin:6px 0; }</style>
</head>
<body>
  <p>Hello</p> 
  how are 
  <p>you?</p>
  <button>Call remove() on paragraphs</button>
</body>
</html>

Removes all paragraphs that contain "Hello" from the DOM

    $("button").click(function () {
      $("p").remove(":contains('Hello')");
    });

<!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(){
    
    $("button").click(function () {
      $("p").remove(":contains('Hello')");
    });

  });
  </script>
  <style>p { background:yellow; margin:6px 0; }</style>
</head>
<body>
  <p class="hello">Hello</p>
  how are 
  <p>you?</p>
  <button>Call remove(":contains('Hello')") on paragraphs</button>
</body>
</html>

NameType