简体   繁体   English

从Textarea添加部分文本以某种方式着色/突出显示

[英]Adding part of the text from Textarea colored/highlighted somehow

Well, I would like to hightlight part of the text in a textarea tag, like facebook does it when you are linking someone in your status update. 好吧,我想在textarea标签中高亮显示部分文本,就像facebook在您的状态更新中链接某人时那样做。 how do I do that? 我怎么做? Thanks in advance. 提前致谢。

What facebook do is they have a div sitting under the textarea which updates on keyup on from the textarea, which can obviously use tags and css to change how the text looks, they wrap the highlighted words in b tags which have background: lightbluecolor ; facebook做的是他们在textarea下面有一个div,它更新了textarea上的keyup,显然可以使用标签和css来改变文本的外观,它们将突出显示的单词包装在b标签中,背景为: lightbluecolor ; font-weight:normal 字体重量:正常

So your markup would look something like this 所以你的标记看起来像这样

<div class="highlight">
    hi <b>Dave<b> how are you?
</div>

<textarea></textarea>

.hightlight { position:absolute }
.hightlight b { font-weight:normal; background:#FEFAE2 }

$('textarea').keyup(function(){
    // do stuff with detecting the mentions but essentially:
    $('.highlight').html($(this).val());
})

If jQuery is in your stack, this plugin should do the trick for you: 如果jQuery在你的堆栈中,这个插件应该为你做的诀窍:

https://github.com/mistic100/jQuery-highlightTextarea https://github.com/mistic100/jQuery-highlightTextarea

You're going to need the help of Javascript to do this. 您将需要Javascript的帮助才能执行此操作。 Because the solution is, I believe, a bit too complex for this Q&A site, I would suggest Googling tutorials, for example: http://www.developphp.com/view.php?tid=1192 . 因为我认为该解决方案对于此问答网站来说有点过于复杂,我建议使用Google搜索教程,例如: http//www.developphp.com/view.php?tid = 1192。 There are also some great examples for you to examine: http://www.webdesignerdepot.com/2008/12/20-excellent-free-rich-text-editors/ 您还可以查看一些很好的示例: http//www.webdesignerdepot.com/2008/12/20-excellent-free-rich-text-editors/

Use this jquery plugin http://plugins.jquery.com/project/htmlArea 使用此jquery插件http://plugins.jquery.com/project/htmlArea

from the website 来自网站

With this very basic plug-in, you can allow any HTML inside a textarea - with full jQuery UI Theme support. 使用这个非常基本的插件,您可以在textarea中允许任何HTML - 具有完整的jQuery UI Theme支持。

It's as simple as $("TEXTAREA").htmlArea(); 它就像$(“TEXTAREA”)一样简单.htmlArea();

There are many options you can set, the colour of the text, position, show formatting options etc. etc. 您可以设置许多选项,文本颜色,位置,显示格式选项等。

$("TEXTAREA").htmlArea({color:'#FF00FF',align:'right'}); $( “TEXTAREA”)htmlArea({颜色: '#FF00FF',对齐: '右'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM