【问题标题】:Regex pattern to change the color in all occurences正则表达式模式以更改所有出现的颜色
【发布时间】:2022-01-19 07:41:06
【问题描述】:
"<p><span style="color: rgb(255, 255, 255);">Beautifully hand designed brochure with arresting imagery created by the designers</span><em style="color: rgb(255, 255, 255);"> eminem.</em></p>"
"<p><span style="color: rgb(105,105,105);">Beautifully hand designed brochure with arresting imagery created by the designers</span><em style="color: rgb(255, 255, 255);"> eminem.</em></p>"

我想通过编写脚本来更改 rgb 颜色 (128,128,128)。请帮我解决这个问题

【问题讨论】:

  • 但是你不需要正则表达式来改变颜色,你可以用 .css('color', new_color) 改变元素的颜色。由于某种原因,您需要正则表达式吗?
  • 因为我需要替换所有出现的特定字符串。如果还有其他方法。请告诉我

标签: javascript regex regexp-replace


【解决方案1】:

您可以使用此 javascript 代码将所有跨度更改为首选 rgb 颜色:

document.querySelectorAll('span').forEach(element => {
  element.style.color = 'rgb(128,128,128)';
});
<p><span style="color: rgb(255, 255, 255);">Beautifully hand designed brochure with arresting imagery created by the designers</span><em style="color: rgb(255, 255, 255);"> eminem.</em></p>
<p><span style="color: rgb(105,105,105);">Beautifully hand designed brochure with arresting imagery created by the designers</span><em style="color: rgb(255, 255, 255);"> eminem.</em></p>

您还可以将“span”更改为您想要选择和更改的任何元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-11-19
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多