gpt4 book ai didi

javascript - 使用换行添加动态 html 父级

转载 作者:行者123 更新时间:2023-11-28 15:20:10 26 4
gpt4 key购买 nike

<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>

我愿意$('p').wrap('<div class="paragraph"></div>')但得到了 4 div包装每个p ,如何只用 1 个 wrapper 来包装每个 p

最佳答案

您需要 .wrapAll() 而不是 .wrap()

.wrap(): Wrap an HTML structure around each element in the set of matched elements. .wrapAll(): Wrap an HTML structure around all elements in the set of matched elements.

$('p').wrapAll('<div class="paragraph"></div>');

<强> Working Demo

关于javascript - 使用换行添加动态 html 父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874684/

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