• 회원가입
  • 로그인
  • 구글아이디로 로그인

[HTML_CSS] JQ - wrap() 메서드 ★ - 선택요소를 지정 부모요소로 감싸기. (= wrap메서드 = 랩메서드)

목차

  1. wrap() 예제 - 각각의 p요소를 div 요소로 감싸기
  2. wrap() 정의
  3. wrap() 구문
  4. wrap() 예제 - DOM 이용해 요소 생성해 감싹기
  5. wrap() 예제 - 함수로 요소 생성해 감싸기
  6. wrap() 예제 - iframe 요소를 div 요소로 감싸기

 

wrap() 예제 - 각각의 p요소를 div 요소로 감싸기

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<script>

$(document).ready(function(){

  $("button").click(function(){

    $("p").wrap("<div></div>");

  });

});

</script>


<style>

div{background-color: yellow;}

</style>


<p>홈짱닷컴 Homzzang.com</p>

<p>홈페이지 제작관리 강의</p>

<button>클릭</button>

 

결과보기

 

wrap() 정의

 

선택요소 각각의 주위를 지정 HTML 요소로 감싸기.

 


 

cf.

wrapAll() 메서드 : 선택요소 모두의 주위를 지정 HTML 요소로 감싸기.

unwrap() 메서드 : 선택요소를 감싸는 부모 HTML 요소 제거.

 

 

wrap() 구문

 

$(selector).wrap(wrappingElement,function(index))

 


[매개변수]

 

wrappingElement

필수. 선택 요소를 감쌀 HTML 요소 지정.

(가능한 값 종류)

HTML elements

jQuery objects

DOM elements

 

function(index)

선택. 감싸는 요소를 반환하는 함수 지정.

index : 세트에서 요소의 색인 위치 반환.

 

 

wrap() 예제 - DOM 이용해 요소 생성해 감싹기

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<script>

$(document).ready(function(){

  $("button").click(function(){

    $("p").wrap(document.createElement("div"));

  });

});

</script>


<style>

div{background-color: yellow;}

</style>


<p>홈짱닷컴 Homzzang.com</p>

<p>홈페이지 제작관리 강의</p>


<button>클릭</button>

 

결과보기

 

wrap() 예제 - 함수로 요소 생성해 감싸기

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<script>

$(document).ready(function(){

  $("button").click(function(){

    $("p").wrap(function(){

        return "<div></div>"

    });

  });

});

</script>


<style>

div{background-color: yellow;}

</style>


<p>홈짱닷컴 Homzzang.com</p>

<p>홈페이지 제작관리 강의</p>


<button>클릭</button>

 

결과보기

 

wrap() 예제 - iframe 요소를 div 요소로 감싸기

 

<script src="http://code.jquery.com/jquery-latest.js"></script>


<iframe src="https://gfycat.com/ifr/thunderousaridgenet" frameborder="0" scrolling="no" width="560" height="444"></iframe>


<script type="text/javascript">

(function($){    

    $('iframe[src^="https://gfycat.com/"]').each(function(){

        $(this).wrap('<div class="gfycatWrap"></div>');

    });

})(jQuery);

</script>

 

결과보기


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
HTML_CSS JQ - val() 메서드 ★★★ - value속성 값설정/값반환 (= val메서드 = 밸메서드) ※ 밸류메…
HTML_CSS JQ - width() 메서드 ★★★ - 요소 너비 설정/반환. (= width메서드 = 위드스메서드)
HTML_CSS JQ - wrap() 메서드 ★ - 선택요소를 지정 부모요소로 감싸기. (= wrap메서드 = 랩메서드)
HTML_CSS JQ - wrapAll() 메서드 - 선택요소 모두를 한번에 지정 요소로 감싸기. (= wrapAll메서드 …
HTML_CSS JQ - wrapInner() 메서드 - 선택요소 안 내용을 지정 요소로 감싸기 (= wrapInner메서드…
3/3
목록
찾아주셔서 감사합니다. Since 2012