【问题标题】:change color of special characters in HTML更改 HTML 中特殊字符的颜色
【发布时间】:2015-12-31 09:00:30
【问题描述】:

我有一个 Unicode HTML 页面(保存在数据库中),无论如何我可以通过编程方式更改所有“。”的颜色。和文本中的“:”字符(请注意,我的 HTML 内容还包含可能包含“。”或“:”字符的内联 CSS,但我只想更改实际文本中提到的字符的颜色。

我有什么选择?一种方法是在文本中找到这些字符并将它们放在标签中,以便可以设置样式,还有其他建议吗? (如果我要使用这种方法,如何区分HTML/CSS字符和文本中的真实字符?)我使用的是ASP.NET/C#

【问题讨论】:

  • 可以在 Question 中包含 html , css 吗?
  • 如果您只想要 asp.net 解决方案,您不应该至少删除 Javascript 标记吗?对不起,如果我弄错了
  • 我认为基于 JS/CSS 的解决方案会更好/更容易实现,我提到 ASP.NET 只是为了说明我的编程方法。任何可以解决问题的解决方案都将受到高度欢迎!
  • 我不熟悉 C# 或 ASP.NET,但我熟悉 PHP。您可以尝试在 PHP 中找到与此功能等效的功能; <?php $charToSearch = ':'; echo str_replace($charToSearch,"<span style='color:red;' class='editorGenerated'>".$charToSearch."</span>",$stringFromDB); ?>

标签: javascript html css asp.net


【解决方案1】:

尝试使用 String.prototype.replace()RegExp /\.|:/g ,返回 i 元素并将 style 属性设置为特定颜色

var div = document.getElementsByTagName("div")[0];
div.innerHTML = div.innerHTML.replace(/\.|:/g, function(match) {
  return "<i style=color:tomato;font-weight:bold>" + match + "</i>"
})
<head>
  <meta charset="utf-8" />
</head>

<body>
  <div>
    I've a Unicode HTML page (saved in DB), is there anyway that I can programmatically change color of all "." and ":" characters in text (please pay attention that my HTML content has also inline CSS which may contain "." or ":" characters, but I just want
    to change color of the mentioned characters in real text. what are my options? One way can be finding these characters in the text and put them in tag, so that can be styled, any other suggestion? (if I'm going to use this method, how can I distinguish
    between HTML/CSS characters and real characters in the text?) I'm using ASP.NET/C#
  </div>
</body>

【讨论】:

    【解决方案2】:

    这是改变 HTML 语言中任何字符颜色的简单方法

    “空间字符”

    【讨论】:

    • *
    猜你喜欢
    • 2022-11-24
    • 2016-03-04
    • 2023-03-09
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多