我在 HTML 中有这段代码:
<a class="cta" href="URL" target="_blank">TEXT</a>
我试图实现的是在链接标签内添加一个 span
标签,这样它看起来像这样:
<a class="cta" href="URL" target=_"blank"><span>TEXT</span></a>
我试过前置,但它似乎不适合我的情况……
最佳答案
使用 jQuery 的 .wrapInner( wrappingElement )
:
$(".cta" ).wrapInner("<span></span>");
Description: Wrap an HTML structure around the content of each element in the set of matched elements.
The
.wrapInner()
function can take any string or object that could be passed to the$()
factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. The structure will be wrapped around the content of each of the elements in the set of matched elements.
关于javascript - 如何使用 jQuery 在链接标记内添加跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261456/