How to only select text outside of a tag in jQuery

萝らか妹 提交于 2019-12-02 08:15:43

I deleted my first post to avoid the confusion, although it answered the topic question. ) But in your case a different approach should be used, I suppose:

1) get all the text nodes by...

var textNodes = $('body').find('*').contents().filter(function() { 
  return this.nodeType === 3 });

2) highlight the search term with some replacement op:

var term = 'xxx';
textNodes.each(function() {
  var $this = $(this);
  var content = $this.text();
  content = content.replace(term, '<span class="highlight">' + term + '</span>');
  $this.replaceWith(content);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!