包裹節(jié)點,可以理解為就是套上一個標簽,比方說給li標簽套上一個sapn,讓li標簽套上一個strong等。
方法為:
wrap方法:wrap()包裹整個
//方法1:wrap()
//語法:$('被包裹元素').wrap('被什么包裹')
//寫法:$('a').wrap('li') a標簽被li標簽包裹
-
//方法2:wrapInter()包裹內容部分
//語法:$('被包裹的元素').wrapInter('<加上的特效或某個標簽>')
//寫法:$('a').wrapInter('<strong>)a標簽的內容被strong標簽給包裹了
-
//方法3:wrapAll()包裹一整個節(jié)點、比方說li的整個節(jié)點被ul包裹,a標簽被 div包裹
//語法:$('包裹的目標'),warpAll('<包裹上什么標簽>')
//寫法:$('li').wrapAll('<ul style="background=red">')
-
//方法4:unwrap()解套操作,接觸包裹屬性,只能解除加上的包裹,不需要賦予參數
//語法:$('目標').unwrap()
//寫法:$('li').unwrap()
代碼部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<a href="">這是一個a標簽</a>
<a href="">這是一個a標簽</a>
<a href="">這是一個a標簽</a>
<a href="">這是一個a標簽</a>
<br />
<button>wrap方法</button>
<button>wrapinner方法</button>
<button>wrapAll方法</button>
<button>Unwrap的方法</button>
<p>這個是一個段落</p>
</body>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//方法1:wrap()
//語法:$('被包裹元素').wrap('被什么包裹')
$('button').eq(0).on('click',function(){
$('a').wrap('<li>')
})
//wrapinner()包裹每個節(jié)點的內容
//$('被包裹元素').wrapInner[選中被包裹元素的內容部分]('被被包括內容套上個什么?')
$('button').eq(1).on('click',function(){
$('li').wrapInner('<strong>')
})
//wrapAll()包括整個一組的節(jié)點
//語法:warpAll().wrapAll('<ul style="background:red">')
$('button').eq(2).on('click',function(){
$('li').wrapAll('<ul style="background-color:red">')
})
//Unwrap():解套操作,用于解除代碼上的所有集合
//語法:$('li').unwrap
$('button').eq(3).on('click',function(){
$('li').unwrap()
})
</script>
</html>
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號