是否有可能以这样一种方式应用 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/