javascript - 通过 style 标签使用 JQuery 修改 html 内容

标签 javascript jquery html

我正在尝试根据样式标签编辑 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/

相关文章:

c# - 在 JavaScript 中编码字符串

javascript - that = $(this) 表示 setTimeout 闭包未按预期工作

javascript - 从外部js文件获取变量

javascript - req.send ('null' ) 不是 Node.js mySQl 的函数

html - 查询标题图片。 HTML 和 CSS 是正确/最有效的方法吗?

javascript - 创建 extjs 存储数据记录失败

javascript - 循环遍历元素jquery选择器

javascript - if 语句 javascript on select 标签

javascript - 初学者 - While 循环将所有用户输入求和在 50 以下 - Javascript

javascript - 提交后禁用上传页面