使用 jQuery 实时突出显示单词
我想用 jQuery 来突出显示单词。我使用实时 keyup 事件来触发突出显示,以便突出显示在输入单词时发生变化(为此使用输入文本字段)
我找到了一段可以正常工作的代码,但此代码不适用于实时 keyup 事件,因此它会包裹第一个字母并停在那里。
这是我的 HTML:
<input type="text" id="input" name="input"/>
<div id="content-block"><p>lorem ip sum </p></div>
这是我的 JS:
$('#input').live('keyup', function() {
$('#input').trigger('highLight');
});
$('#input').bind('highLight', function() {
var o = {words:$('input[name="input"]').val()};
highlight("content-block", o);
});
function highlight(id, options) {
var o = {
words: '',
caseSensitive: false,
wordsOnly: true,
template: '$1<span class="highlight">$2</span>$3'
},
pattern;
$.extend(true, o, options || {});
if (o.words.length == 0) { return; }
pattern = new RegExp('(>[^<.]*)(' + o.words + ')([^<.]*)', o.caseSensitive ? "" : "ig");
$('#'+id).each(function() {
var content = $(this).html();
if (!content) return;
$(this).html(content.replace(pattern, o.template));
});
}
I want to live highlight words with jQuery. I use a live keyup event to trigger the highlighting, so that the highlighting changes upon entering words(using a input text field for this)
I have found a piece of code which works fine but this code doesn't works with the live keyup event, so it wraps the first letter and stops there.
Here is my HTML:
<input type="text" id="input" name="input"/>
<div id="content-block"><p>lorem ip sum </p></div>
Here is my JS:
$('#input').live('keyup', function() {
$('#input').trigger('highLight');
});
$('#input').bind('highLight', function() {
var o = {words:$('input[name="input"]').val()};
highlight("content-block", o);
});
function highlight(id, options) {
var o = {
words: '',
caseSensitive: false,
wordsOnly: true,
template: '$1<span class="highlight">$2</span>$3'
},
pattern;
$.extend(true, o, options || {});
if (o.words.length == 0) { return; }
pattern = new RegExp('(>[^<.]*)(' + o.words + ')([^<.]*)', o.caseSensitive ? "" : "ig");
$('#'+id).each(function() {
var content = $(this).html();
if (!content) return;
$(this).html(content.replace(pattern, o.template));
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,在第一次成功替换之后,模式现在还必须匹配在第一个匹配字符之后插入的结束范围标记。
输入“l”后,HTML 看起来像这样:
由于跨度,“lorem”将不再匹配。
现在有了解决方案:
这是一个修复程序,应该可以让它执行您想要的操作:
假设您只有一个突出显示范围,下面的行将删除该范围并将其替换为它包含的文本。然后替换就会正常进行。
一个工作示例在这里: http://jsfiddle.net/ryanrolds/N4DCR/
The issue is that after the first successful replace the pattern now has to also match the ending span tag that has been inserted after the first matched character.
The HTML looks like this after entering "l":
"lorem" will no longer match because of the span.
Now with solution:
Here is a fix that should get it doing what you want:
Assuming that you will only have a single highlight span, the line below removes the span and replaces it with the text that it contained. Then the replace happens as normal.
A working example is here: http://jsfiddle.net/ryanrolds/N4DCR/
即使您处于区分大小写的模式,您也应该为正则表达式指定全局标志。这可能就是为什么只有第一个字符被替换的原因。尝试使用这个:
You should specify the global flag for your regular expression even if you are in case-sensitive mode. This is probably why only the first char gets replaced. Try using this: