如何搜索和突出显示不包括标签内内容的术语

how to search and highlight terms excluding content inside a tag?

本文关键字:标签 显示 不包括 术语 何搜索 搜索      更新时间:2023-09-26

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

基本上试图忽略<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";

虽然这会忽略标记,但不会忽略标记中的文本。

如果您知道它总是要删除的sup,那么您应该能够执行类似的操作

function stripSupFromText(text) {
  var div = document.createElement('div');
  div.innerHTML = text;
  div.removeChild(div.getElementsByTagName('sup')[0]);
  return div.innerText;
}