如何删除窗口的背景色.查找突出显示

How to remove backcolor of window.find Highlights

本文关键字:查找 背景色 显示 窗口 何删除 删除      更新时间:2023-09-26

如何回滚由以下代码引起的突出显示

if (window.find && window.getSelection) {
     var sel = window.getSelection(); 
     sel.collapse(document.body, 0);  
     document.body.offsetHeight;
     if (window.find(text, true)) { 
        document.execCommand("hiliteColor", false, "YellowGreen"); 
        sel.collapseToEnd(); 
     }
}

如何删除所有高光背景色,即"黄绿色"。我看过一篇与我的问题相关的帖子。但是接受的答案不起作用。请有人调查一下并帮助我。

我有一个解决方案。你的问题中没有足够的细节来写一些可以插入的东西,所以你可能不得不调整它才能得到你想要的东西。

这个想法是在突出显示代码运行时监视DOMNodeInserted突变事件,并使用可用于查找和删除它们的className标记正在插入的节点。警告:突变事件已弃用,但实际上没有替代品,所以我正在使用我所拥有的。

Highlighter = (function() {
    var highlighting = false;
    document.addEventListener('DOMNodeInserted', function(e) {
        if (highlighting) {
            var target = e.target;
            if (target.nodeType == 1) {
                target.className = CLASS_NAME;
            }
        }
    }, false);
    var CLASS_NAME = 'highlighted';
    return {
        highlight: function(text, color) {
            highlighting = true;            
            var sel = window.getSelection(); 
            sel.collapse(document.body, 0);
            if (window.find(text, true)) { 
                document.execCommand("hiliteColor", false, color); 
                sel.collapseToEnd(); 
            } 
            highlighting = false;
        },
        unhighlight: function() {
            var highlighted = document.querySelectorAll('.' + CLASS_NAME);
            var i = highlighted.length;
            while (i--) {
                var node = highlighted[i];
                node.parentNode.replaceChild(node.firstChild, node);
            }
        }
    }
})();

仅在 Chrome 17 中测试。这是它工作的小提琴:http://jsfiddle.net/LPJqW/

我找到了另一种选择....

$('body *').each(function () {
  ($(this).css('background-color') == "rgb(70, 130, 180)") || ($(this).css('background-color') == "rgb(255, 192, 203)") ? $(this).css("background-color", "") : 0;
});