문서의 이전 판입니다!
contents() 메서드는 선택한 요소의 텍스트 및 주석 노드를 포함한 모든 직계 자식을 반환합니다.
텍스트 노드는 요소가 표시하는 실제 텍스트입니다.
이 메서드는 텍스트 및 주석 노드도 반환한다는 점을 제외하고 children() 메서드와 유사합니다.
Contents() 메서드는 iframe이 동일한 도메인에 있는 경우 HTML에도 액세스할 수 있습니다.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <title>jquery contents()</title> </head> <body> <div> <em>Hello World! What a beautiful day!</em> </div> <p> In this example, by clicking on the button, we search for all the text nodes inside the div element and wrap them with a b element. </p> <button>Find all text nodes in div and wrap them.</button><br /> <script> $(document).ready(function () { $("button").click(function () { $("div").contents().filter("em").wrap("<b />"); }); }); </script> </body> </html>