简体   繁体   English

jQuery或JavaScript如何突出显示文本区域中的选定文本

[英]jQuery or JavaScript how to highlight selected text in a textarea

I'm trying to figure out how Facebook does it. 我试图弄清楚Facebook是如何做到的。 I know they use textarea's in there comments and status features, but if I tag someone in a post or something, it will highlight that selection of text in the textarea with a light blue background. 我知道他们在其中使用了textarea的注释和状态功能,但是如果我在帖子或某物中标记某人,它将以浅蓝色背景突出显示在textarea中选择的文本。 I know textarea's or last I knew rather at least, that this wasn't possible with this type of element, so how are they doing it? 我至少知道textarea的知识,或者我至少知道,这种元素无法做到这一点,那么他们怎么做呢? Whats the technique? 什么技术? anyone have any idea? 有人有什么主意吗? Im banking on some smoke and mirrors as to how its being done, but not sure how? 我想知道如何做,但不确定如何做?

It is not done using a classic textarea. 它不是使用经典的textarea完成的。 I know it is sounds crazy, but as you type, it generates html where it can mark/highlight keywords and moves an input around so you have a place to type. 我知道这听起来很疯狂,但是当您键入内容时,它会生成html,可以在其中标记/突出显示关键字并在输入内容周围移动,以便您可以输入内容。 You can have a look with Firebug to see how it happens in real time. 您可以使用Firebug看看它是如何实时发生的。

I've worked up an example based on the html5 demo that allows you to do this. 我已经完成了一个基于html5演示的示例,使您可以执行此操作。 Check it out http://jsfiddle.net/KStst/ This is just telling the browser to make the section editable and it understands basic html features. 检查一下http://jsfiddle.net/KStst/这只是告诉浏览器使该部分可编辑,并且它了解html的基本功能。 This is probably not how facebook is doing it but given html5 is "the future" it's probably worth playing with. 这可能不是facebook的工作方式,但是考虑到html5是“未来”,它可能值得一玩。

Although it is using a textarea in some fashion, the bulk of the magic is done with div s and CSS. 尽管它以某种方式使用了textarea ,但是魔术的大部分是由div和CSS完成的。 I pulled this from Safari's Web Inspector: 我从Safari的Web检查器中拉了这个:

<div class="inputContainer">
  <div class="uiMentionsInput" id="up84fa_5">
    <div class="highlighter" style="direction: ltr; text-align: left; ">
      <div style="width: 499px; ">
        <span class="highlighterContent"><b>Brandan Lennox</b> is the coolest.</span>
      </div>
    </div>

    <div class="uiTypeahead composerTypeahead mentionsTypeahead" id="up84fa_9" style="height: auto; ">
      <div class="wrap">
        <input type="hidden" autocomplete="off" class="hiddenInput">
        <div class="innerWrap">
          <textarea class="uiTextareaAutogrow input mentionsTextarea textInput" title="What's on your mind?" name="xhpc_message_text" placeholder="What's on your mind?" onfocus="return wait_for_load(this, event, function() {if (!this._has_control) {  new TextAreaControl(this).setAutogrow(true);  this._has_control = true; } return wait_for_load(this, event, function() {JSCC.get('j4f3584ff4f90d07867222385').init(JSCC.get('j4f3584ff4f90d07867222386'));;JSCC.get('j4f3584ff4f90d07867222386').init([&quot;buildBestAvailableNames&quot;,&quot;hoistFriends&quot;]);JSCC.get('j4f3584ff4f90d07867222383').init({&quot;max&quot;:10}, null, JSCC.get('j4f3584ff4f90d07867222385'));;;});});" autocomplete="off" style="height: 48px; direction: ltr; ">What's on your mind?</textarea>
        </div>
      </div>
    </div>
    <input type="hidden" autocomplete="off" class="mentionsHidden" name="xhpc_message" value="@[815490222:Brandan Lennox] is the coolest.">
  </div>
  <div class="attachmentMetaArea"></div>
</div>

The important elements: 重要元素:

  • The div.highlighter and its descendant span.highlighterContent . div.highlighter及其后代span.highlighterContent Notice that it styles a b element as the highlighted content. 注意,它将b元素设置为突出显示的内容。
  • The messy onfocus handler for the actual textarea element. 实际textarea元素的凌乱的onfocus处理程序。 Somewhere in there, it creates a TextAreaControl object, which seems to create an object somewhere else in the DOM, since the textarea content itself is not being updated. 它在其中的某个位置创建了一个TextAreaControl对象,该对象似乎在DOM中的其他位置创建了一个对象,因为textarea内容本身并未​​更新。
  • The input[type=hidden].mentionsHidden that submits custom data to the server so it knows who you actually mentioned. input[type=hidden].mentionsHidden将自定义数据提交到服务器,以便它知道您实际上提到了谁。

This is totally Facebook specific, but maybe it gives you some ideas for what you'd like to do. 这完全是针对Facebook的,但是也许可以为您提供一些您想做的想法。

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

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