简体   繁体   English

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

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

HTML: 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

I'm trying to match:我正在尝试匹配:

in the mouth. The team went

Basically trying to ignore the text inside the <sup> tag基本上试图忽略<sup>标签内的文本

I've adapted code from this answer .我已经改编了这个答案的代码。

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";

While this ignores the tag, it doesn't ignore text within the tag.虽然这会忽略标签,但不会忽略标签内的文本。

If you know it's always a sup that you want to remove then you should be able to do something like如果您知道它始终是您想要删除的sup ,那么您应该能够执行类似的操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM