2016-09-27 8 views
1

HTML:タグ内のコンテンツを除いた用語を検索して強調表示する方法は?

Our formula is this: We go out, we hit people in the mouth.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9">[9]</a></sup> The team went 5–4 overall

私が一致しようとしている:

in the mouth. The team went

を基本的に私はthis answerからコードを適応してきた<sup>タグ

内のテキストを無視しようとしています。

document.designMode = "on"; 
var sel = window.getSelection(); 
sel.collapse(document.body, 0); 
while (window.find(search_string)) { 
document.execCommand("backColor", false, "yellow"); 
} 
sel.collapseToEnd(); 
document.designMode = "off"; 

これはタグを無視しますが、タグ内のテキストは無視しません。

答えて

1

あなたはそれは常にあなたが削除したいsupを知っているなら、あなたは

function stripSupFromText(text) { 
    var div = document.createElement('div'); 
    div.innerHTML = text; 
    div.removeChild(div.getElementsByTagName('sup')[0]); 
    return div.innerText; 
} 
ような何かを行うことができるはずです
関連する問題