ドキュメント

Traversing/contents

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Traversing

contents( )

Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.

Examples:
Find all the text nodes inside a paragraph and wrap them with a bold tag.

$("p").contents().filter(function(){ return this.nodeType != 1; }).wrap("<b/>");

<!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").contents().filter(function(){ return this.nodeType != 1; }).wrap("<b/>");
  });
  </script>
  
</head>
<body>
  <p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p>
</body>
</html>

Append some new content into an empty iframe.

$("iframe").contents().find("body").append("I'm in an iframe!");

<!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(){
    $("iframe").contents().find("body").append("I'm in an iframe!");
  });
  </script>
  
</head>
<body>
  <iframe src="/index-blank.html" width="300" height="100"></iframe>
</body>
</html>

NameType