gpt4 book ai didi

jquery - 使用 jQuery 包围元素

转载 作者:行者123 更新时间:2023-12-01 05:24:04 24 4
gpt4 key购买 nike

我希望环绕一个 <a><h1></h1> 。我正在尝试:

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

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

最佳答案

您当前的代码不起作用,因为您只能将整个元素附加到 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>

关于jquery - 使用 jQuery 包围元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com