html - 将 CSS 应用于单个单词

标签 html css styles

是否有可能以这样一种方式应用 CSS,即它会检测元素中是否存在特定单词并以某种方式标记该单词?我知道我可以添加 <span style="...">word</span><span class="...">word</span>与 HTML 内联,但这不会自动检测单词。有没有办法完全在 CSS 中做到这一点?感谢您的帮助。

最佳答案

仅靠 css 无法做到这一点。您可以将单词的所有实例替换为包装版本,并根据您的示例应用样式 http://jsfiddle.net/w9VbP/ :

js

var word = 'hello';
var replacement = '<span class="theword">' + word + '</span>';

var re = new RegExp(word, 'ig');

document.body.innerHTML = document.body.innerHTML.replace(re, replacement);

CSS

.theword
{
    color:red;
}

关于html - 将 CSS 应用于单个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380053/

相关文章:

JQuery - 动态 CSS 动画更改仅在我离开浏览器窗口并返回时发生

angular - 仅在 Angular Material 工具提示中的第一行应用样式

c# - WPF:动态更改相同类型的所有用户控件的依赖属性值

android - 我想使用 android L 主题

javascript - 删除 DOM 中的图像时如何防止内存泄漏?

css - 什么可以使复选框不显示在页面上?

html - 如何移动导航文本的位置?

html - 无法在屏幕上水平居中

javascript - 在 div 中的鼠标悬停操作中隐藏和显示图像

php - 在 tcpdf 中使用两种不同类型的字体