목차
wrap() 예제 - 각각의 p요소를 div 요소로 감싸기
wrap() 정의
wrap() 구문
wrap() 예제 - DOM 이용해 요소 생성해 감싹기
wrap() 예제 - 함수로 요소 생성해 감싸기
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>
결과보기
주소 복사
랜덤 이동