【发布时间】:2022-01-19 15:11:00
【问题描述】:
我创建了一个 jQuery sn-p,但在动态显示结果时遇到了一些问题。每个 H2 标签内都有一个图像。下面是我想要完成的脚本:
<h2 id="test">This is a test <img class="test" src="https://www.iconsdb.com/icons/preview/red/new-xxl.png" alt="test" width="31" height="16" /></h2>
还有我的 jQuery:
$('h2 img').each(function(){
var = test $(this).html();
$(this).html(test.replace(/<h2>(.*?)\s<img[^>]+src="([^">]+)(.*)\/><\/h2>/gi, '<h2>$1<img class="test" src="https://www.iconsdb.com/icons/preview/red/new-xxl.png" alt="test" width="31" height="16" /></h2>'));
]);
它应该将图像移动到单词 test 旁边(根据我的正则表达式删除单词 test 和 img 标签之间的额外空格)。
输出应该如下所示(注意:图像标签在单词 test 旁边):
<h2 id="test">This is a test<img class="test" src="https://www.iconsdb.com/icons/preview/red/new-xxl.png" alt="test" width="31" height="16" /></h2>
我正在尝试动态地完成此操作。任何帮助表示赞赏。
【问题讨论】: