高亮显示字符串中的单词 [英] Highlight words inside the string

查看:103
本文介绍了高亮显示字符串中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有确定的字符串,我想突出显示在文本输入中键入的子字符串.

这是一个小提琴: http://jsfiddle.net/rFGWZ/17/

当我键入初始字符串时,但当我键入fe时,它只能正常工作.输入中的15,它不会突出显示15,而是显示21 ...它总是在开始时突出显示数字,所以有人可以帮我修改它,因此它将突出显示我键入的字符串在文本输入中?

解决方案

您可以使用此:

firstCell.html(id.replace(value, '<span class=highlight>'+value+'</span>'));

代替

firstCell.html($("<span />").addClass("highlight").text(id.slice(0, value.length)));
firstCell.append(id.slice(value.length, id.length));

演示: http://jsfiddle.net/qgBt8/

不区分大小写的版本

I got certain string, and I want to highlight the substring typed in the text input.

Here is a fiddle: http://jsfiddle.net/rFGWZ/17/

It is working fine only , when I type the initial string, but when I type fe. 15 in the input, it doesn't highlight the 15 but 21 instead... it is always highlighting the numbers at the beggining, so could someone help me modify it, so it will highlight the string I type in the text input?

解决方案

You can use this :

firstCell.html(id.replace(value, '<span class=highlight>'+value+'</span>'));

instead of

firstCell.html($("<span />").addClass("highlight").text(id.slice(0, value.length)));
firstCell.append(id.slice(value.length, id.length));

Demo : http://jsfiddle.net/qgBt8/

EDIT : case insensitive version

这篇关于高亮显示字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆