목차

jQuery contents() Method

  • description : jQuery contents() Method
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2022-10-24 Mon


The source of this article

jQuery contents() Method

Definition and Usage

contents() 메서드는 선택한 요소의 텍스트 및 주석 노드를 포함한 모든 직계 자식을 반환합니다.

텍스트 노드는 요소가 표시하는 실제 텍스트입니다.

이 메서드는 텍스트 및 주석 노드도 반환한다는 점을 제외하고 children() 메서드와 유사합니다.
Contents() 메서드는 iframe이 동일한 도메인에 있는 경우 HTML에도 액세스할 수 있습니다.

  <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>


<jqeury 적용 전>

<div>
    <em>Hello World! What a beautiful day!</em>
</div>


<jquery 적용 후>

<div>
    <b>
        <em>Hello World! What a beautiful day!</em>
    </b>
</div>

jQuery filter() Method