突出显示搜索项(仅选择叶节点)
我想在页面上突出显示搜索词,但不要弄乱任何 HTML 标签。我在想类似的事情:
$('.searchResult *').each(function() {
$(this.html($(this).html().replace(new RegExp('(term)', 'gi'), '<span class="highlight">$1</span>'));
)};
但是, $('.searchResult *').each
匹配所有元素,而不仅仅是叶节点。换句话说,某些匹配的元素内部包含 HTML。所以我有几个问题:
- 如何只匹配叶节点?
- 是否有一些内置的 jQuery RegEx 函数可以简化事情?像这样的东西:
$(this).wrap('term', $('', { 'class': 'highlight' }))
- 有没有办法做到简单的字符串替换而不是正则表达式?
- 还有其他更好/更快的方法吗?
非常感谢!
I would like to highlight search terms on a page, but not mess with any HTML tags. I was thinking of something like:
$('.searchResult *').each(function() {
$(this.html($(this).html().replace(new RegExp('(term)', 'gi'), '<span class="highlight">$1</span>'));
)};
However, $('.searchResult *').each
matches all elements, not just leaf nodes. In other words, some of the elements matched have HTML inside them. So I have a few questions:
- How can I match only leaf nodes?
- Is there some built-in jQuery RegEx function to simplify things? Something like:
$(this).wrap('term', $('<span />', { 'class': 'highlight' }))
- Is there a way to do a simple string replace and not a RegEx?
- Any other better/faster way of doing this?
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
[查看实际操作]
[See it in action]
使用
contents()
1, < super>2,3 获取包括文本节点在内的所有节点,过滤掉非文本节点,最后使用正则表达式替换每个剩余文本节点的nodeValue
。这将保持 html 节点不变,并且只修改文本节点。您必须使用正则表达式而不是简单的字符串替换,因为不幸的是,当搜索词是字符串时,我们无法进行全局替换。我们现在无法轻松地将其变成一行且更短,所以我更喜欢这样:)
请参阅此处的示例。
Use
contents()
1, 2, 3 to get all nodes including text nodes, filter out the non-text nodes, and finally replace thenodeValue
of each remaining text node using regex. This would keep the html nodes intact, and only modify the text nodes. You have to use regex instead of simple string substitutions as unfortunately we cannot do global replacements when the search term is a string.We can't make it a one-liner and much shorter easily now, so I prefer it like this :)
See example here.
我会给出突出显示 jQuery 插件一击。
I'd give the Highlight jQuery plugin a shot.
我已经制作了一个纯 JavaScript 版本,并将其打包到 Google Chrome 插件中,希望对某些人有所帮助。核心功能如下所示:
页内荧光笔的 GitHub Page
I've made a pure JavaScript version of this, and packaged it into a Google Chrome plug-in, which I wish to be helpful to some people. The core function is shown below:
GitHub Page for In-page Highlighter
我花了几个小时在网上搜索可以在用户输入时突出显示搜索词的代码,但没有人可以做我想做的事,直到我将一堆东西组合在一起来做到这一点(jsfiddle 演示在这里):
I spent hours searching the web for code that could highlight search terms as the user types, and none could do what I wanted until I combined a bunch of stuff together to do this (jsfiddle demo here):
这是一个简单的实现,它只在 HTML 中爆炸任何匹配:
Here's a naive implementation that just blasts in HTML for any match:
我的声誉不够高,无法发表评论或添加更多链接,因此很抱歉在没有所有参考文献的情况下编写一个新答案。
我对上述解决方案的性能感兴趣,并添加了一些代码测量。为了简单起见,我只添加了这些行:
我用这些行分叉了 Anurag 的解决方案,这导致平均 40-60 毫秒。
所以我分叉了这个小提琴并做了一些改进以满足我的需求。一件事是 RegEx 转义(请参阅 stackoverflow 中“escape-string-for-use-in-javascript-regex”中 CoolAJ86 的答案)。
另一点是防止第二个“new RegExp()”,因为 RegExp.test 函数应该忽略全局标志并返回第一个匹配(请参阅 RegExp.test 上的 javascript 参考)。
在我的机器(chromium、linux)上,我的运行时间约为 30-50 毫秒。您可以在此 jsfiddle 中自行测试。
我还将我的计时器添加到了评分最高的 galambalazs 解决方案中,您可以在 jsFiddle 中找到它。但这个的运行时间为 60-100 毫秒。
在运行时,以毫秒为单位的值变得更高且更重要(例如,在 Firefox 中约为四分之一秒)。
My reputation is not high enough for a comment or adding more links, so I am sorry to write a new answer without all references.
I was interested in the performance of the mentioned solutions above and added some code for measurement. To keep it simple I added only these lines:
I have forked the solution of Anurag with these lines and this resulted in 40-60ms in average.
So I forked this fiddle and made some improvements to fit my needs. One thing was the RegEx-escaping (plz see the answer from CoolAJ86 in "escape-string-for-use-in-javascript-regex" in stackoverflow).
Another point was the prevention of a second 'new RegExp()', as the RegExp.test-function should ignore the global flag and return on the first matching (plz see javascript reference on RegExp.test).
On my machine (chromium, linux) I have runtimes about 30-50ms. You can test this by yourself in this jsfiddle.
I also added my timers to the highest rated solution of galambalazs, you can find this in this jsFiddle. But this one has runtimes of 60-100ms.
The values in milliseconds become even higher and of much more importance when running (e.g. in Firefox about a quarter of a second).