javascript - 如何使用 jQuery 在链接标记内添加跨度?

标签 javascript jquery html hyperlink anchor

我在 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.

Source: https://api.jquery.com/wrapInner/

关于javascript - 如何使用 jQuery 在链接标记内添加跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261456/

相关文章:

javascript - 弹出对话框在 10 秒后隐藏

javascript - 将IRC日志写入文件,每天一个文件

javascript - ckeditor 自动删除空跨度

javascript - JavaScript 数组中的对象神秘地链接在一起

jquery - 如何将多个模板之一的 Meteor 助手设置为 true?

javascript - 排序多维数组的最佳方法

javascript - 混合纹理 (aframe/js)

JavaScript 数字不会显示

javascript - jQuery 无法从另一个按钮单击触发按钮单击

javascript - 如何阻止此 JavaScript 代码删除表格的原始/第一行?