简体   繁体   English

使用 JavaScript 和 CSS 查找自定义文本并更改其颜色

[英]Find a custom text and change its color using JavaScript and CSS

I'm building a site and just met the limitations of WordPress and custom theme, tho I can inject custom Js or CSS easily, so my question is...我正在构建一个站点,刚刚遇到 WordPress 和自定义主题的限制,我可以轻松注入自定义 Js 或 CSS,所以我的问题是......

How to find in content and set its color如何在内容中找到并设置其颜色

More about symbol https://www.htmlsymbols.xyz/unicode/U+2605更多关于符号https://www.htmlsymbols.xyz/unicode/U+2605

var headings = document.evaluate("//li[contains(., '★')]", document, null, XPathResult.ANY_TYPE, null );
var thisHeading = headings.iterateNext();
thisHeading.setAttribute('style', 'color: #f1c40f');

Got it!知道了! I'm leaving the tags below in case someone searches for this solution如果有人搜索此解决方案,我将保留下面的标签

css, unicode, color, javascript, find css、unicode、颜色、javascript、查找

You can loop through the entire dom and search for matches.您可以遍历整个 dom 并搜索匹配项。

let allElements = document.getElementsByTagName("*");
    for (let Element of allElements) {
        for (let childNode of Element.childNodes) {
            if (childNode.nodeType === 3) {
                let valueUpper = childNode.nodeValue.toUpperCase()
                if (valueUpper.includes(text)) {
                    Element.style.backgroundColor='#ff0000';
                    Element.style.color='#ffffff';
                }
            }
        }
    }

I used this code: https://stackoverflow.com/a/6132212/8781059我使用了这段代码: https://stackoverflow.com/a/6132212/8781059

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM