我正在尝试根据样式标签编辑 html 内容
这是html的部分:
<!DOCTYPE html>
<html>
<body>
<span style="color:restest">Testing</span>
<br>
<span style="color:restest">Testing Again</span>
</body>
</html>
我想用 style="color:retest" 修改 span 的所有实例,并在内容中添加 html 标签。
它们应该变成这样:
<a href="example.com/testing">Testing</a>
<a href="example.com/testing_again">Testing_Again</a>
最佳答案
最简单的方法是使用 jQuery#replaceWith
:
$('span[style="color:restest"]').each((i,e) => {
$(e).replaceWith(`<a href="example.com/${e.innerHTML}">${e.innerHTML}</a>`);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="color:restest">Testing</span>
<span style="color:restest">Testing Again</span>
关于javascript - 通过 style 标签使用 JQuery 修改 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47658372/