【发布时间】:2013-04-10 02:51:31
【问题描述】:
好的,我正在尝试在我的 HTML 页面中的搜索文本框中加粗搜索词。我在这里使用 jQuery highlight 插件jQuery highlight plugin 来尝试实现这一点。我仍然不精通 jQuery,如果这听起来太愚蠢,请原谅我
问题是它只突出显示在文本框中输入的第一个字符,其余的被忽略。
- highlight.js 可以找到here
-
这是我的代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Test page for Highlight Search Terms</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="highlight.js" type="text/javascript"></script> <script> $(document).ready(function(){ $('#search').on('keyup', function(){ $('.x').highlight($(this).val()); }); }); </script> <style type="text/css"> .highlight { font-weight: bold; } </style> <body> <input type="text" id="search"></input> <p class="x">Highlight plugin searches through the text of given elements in this case to find the term (word 'highlight' in our example) to highlight. When the term is found it's simply wrapped with </p> </body> </html>
我希望它匹配段落中文本框中的每个搜索词并将其加粗。我做错了什么?谢谢
【问题讨论】:
-
尝试取消突出显示段落,然后再进行更多突出显示。
-
感谢工作完美!