Javascript 如何删除突出显示的单词?

Javascript 如何删除突出显示的单词?,javascript,jquery,html,css,jsp,Javascript,Jquery,Html,Css,Jsp,你好,世界 函数doSearch(文本){ if(window.find&&window.getSelection){ document.designMode=“on”; var sel=window.getSelection(); 选择折叠(document.body,0); while(window.find(text)){ document.execCommand(“HiliteColor”,false,“黄色”); 选择折叠结束(); } document.designMode=“关闭”

你好,世界

函数doSearch(文本){ if(window.find&&window.getSelection){ document.designMode=“on”; var sel=window.getSelection(); 选择折叠(document.body,0); while(window.find(text)){ document.execCommand(“HiliteColor”,false,“黄色”); 选择折叠结束(); } document.designMode=“关闭”; }else if(document.body.createTextRange){ var textRange=document.body.createTextRange(); while(textRange.findText(text)){ execCommand(“BackColor”,false,“yellow”); textRange.collapse(false); } } }
嘿,伙计们,我现在面临一个问题。如何在我再次按下“查找”按钮后删除突出显示的单词?

您需要检查是否已经有一个元素具有突出显示的类。如果是,则首先删除content div p元素内所有高亮显示的跨距

<input type="text" id="search">
<input type="button" id="button" onmousedown="doSearch(document.getElementById('search').value)" value="Find">

<div id="content">
<p>Hello World!</p>


function doSearch(text) {
if (window.find && window.getSelection) {
    document.designMode = "on";
    var sel = window.getSelection();
    sel.collapse(document.body, 0);

    while (window.find(text)) {
        document.execCommand("HiliteColor", false, "yellow");
        sel.collapseToEnd();
    }
    document.designMode = "off";
} else if (document.body.createTextRange) {
    var textRange = document.body.createTextRange();
    while (textRange.findText(text)) {
        textRange.execCommand("BackColor", false, "yellow");
        textRange.collapse(false);
    }
}
}
函数数据搜索(文本){
if(document.querySelector('span[style*=“背景色:黄色;”])!==null){
var elem=document.querySelector(“#content p”);
var txt=elem.innerText | | elem.textContent;
elem.innerHTML=txt;
}
if(window.find&&window.getSelection){
document.designMode=“on”;
var sel=window.getSelection();
选择折叠(document.body,0);
while(window.find(text)){
document.execCommand(“HiliteColor”,false,“黄色”);
选择折叠结束();
}
document.designMode=“关闭”;
}else if(document.body.createTextRange){
var textRange=document.body.createTextRange();
while(textRange.findText(text)){
execCommand(“BackColor”,false,“yellow”);
textRange.collapse(false);
}
}
}

你好,世界

 if(document.querySelector('span[style*="background-color: yellow;"]') !== null) {
        var elem = document.querySelector("#content p");
        var txt = elem.innerText || elem.textContent;
        elem.innerHTML = txt;
   }