2016-11-29 91 views
2

我希望围绕<a><h1></h1>。我想:使用jQuery围绕元素

<a class="toto">TEST</a> 
var test = $('.toto'); 
test.before('<h2>'); 
test.after('</h2>'); 

但是它不工作。我怎样才能做到这一点?

回答

0

您当前的代码不起作用,因为您只能将整个元素附加到DOM。的你想会是这样什么输出:

<h2></h2> 
<a class="toto">TEST</a> 
<h2></h2> 

要做到你需要什么,可以使用wrap()

$('.toto').wrap('<h2 />'); // or .wrap('<h1 />');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a class="toto">TEST</a>

+0

是的!它的工作感谢您的帮助! – Alexis

+0

没问题,很乐意帮忙。 –