[英]how to highlight part of textarea html code
I want to achieve a python version web regexbuddy,and i encounter a problem,how to highlight match values in different color(switch between yellow and blue) in a textarea,there has a demo what exactly i want on http://regexpal.com ,but i was a newbie on js,i didn't understand his code meaning. 我想实现一个python版本的网络regexbuddy,并且遇到一个问题,如何在文本区域中突出显示不同颜色(在黄色和蓝色之间切换)的匹配值,并在http:// regexpal上进行了演示。 com ,但是我是js的新手,我不理解他的代码含义。 any advice is appreciated
任何建议表示赞赏
To save time you should consider using an existing library for this requirement. 为了节省时间,您应该考虑使用现有库来满足此要求。
As textarea elements can't render HTML, this plugin allows you to highlight text inside textarea elements.
由于textarea元素无法呈现HTML,因此该插件可让您突出显示textarea元素内的文本。
jQuery highlightTextarea . jQuery highlightTextarea 。
Usage: 用法:
$context.highlightTextarea(options);
There is a pre
element over the textarea. 文本区域上方有一个
pre
元素。 So when you type anything it is copying the input on the pre
element, applying some filters. 因此,当您键入任何内容时,它将复制输入到
pre
元素上,并应用一些过滤器。
For example: 例如:
<pre id="view"></pre>
<textarea id="code"></textarea>
When you type on #code
it is copying the value, applying filters and adding the HTML to the #view
. 当您在
#code
键入内容时,它会复制值,应用过滤器并将HTML添加到#view
。
var code = document.getElementById("code");
var pre = document.getElementById("pre");
(code).onkeyup = function (){
val = this.value;
val = YourRegex(val);
(pre).innerHTML = val;
};
YourRegex
would be a method to match the regex
and return some parsed content to the pre
, allowing you to customize the appearance of the textarea
(that is actually an element over it). YourRegex
将是一种匹配regex
并将解析后的内容返回到pre
,允许您自定义textarea
的外观(实际上是其上方的元素)。
function YourRegex(val)
{
// This function add colors, bold, whatever you want.
if (/bbcc/i.test("bbcc"))
return "<b>" + val + "</b>";
}
@BrunoLM's solution is excellent, but might require more hacking than you're comfortable with. @BrunoLM的解决方案非常出色,但可能需要更多的黑客技巧,而不是您满意的。 If you're interested (and if jQuery is already in your stack), the following plugin may be worth taking a look at:
如果您有兴趣(并且jQuery已经在您的堆栈中),那么以下插件可能值得一看:
http://garysieling.github.io/jquery-highlighttextarea/ http://garysieling.github.io/jquery-highlighttextarea/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.