簡體   English   中英

使用 JavaScript 和 CSS 查找自定義文本並更改其顏色

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

我正在構建一個站點,剛剛遇到 WordPress 和自定義主題的限制,我可以輕松注入自定義 Js 或 CSS,所以我的問題是......

如何在內容中找到並設置其顏色

更多關於符號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');

知道了! 如果有人搜索此解決方案,我將保留下面的標簽

css、unicode、顏色、javascript、查找

您可以遍歷整個 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';
                }
            }
        }
    }

我使用了這段代碼: https://stackoverflow.com/a/6132212/8781059

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM