admin管理员组文章数量:1314320
I want to highlight the text
inside the textarea
as facebook does. A solution to this is to keep an external div and highlight, as in this fiddle. But then there might be multiple highlighted field and then there position and width will have to be calculated at every instant, which seems very plex. Also backspace should remove the plete tag at once. Any way in which these hashtags
can be implemented or any sample code implementing the same technique above?
I want to highlight the text
inside the textarea
as facebook does. A solution to this is to keep an external div and highlight, as in this fiddle. But then there might be multiple highlighted field and then there position and width will have to be calculated at every instant, which seems very plex. Also backspace should remove the plete tag at once. Any way in which these hashtags
can be implemented or any sample code implementing the same technique above?
- Here is an angularjs directive which creates a content editable div with support for facebook like hashtags highlights github./sujeet100/ngHashtags Similar logic can be used to create what you want without using angularjs. – Sujit Kamthe Commented Jan 13, 2017 at 6:09
2 Answers
Reset to default 10The highlighting is occurring on a background layer that is updated on a keyup event listener (the textarea
is transparent (background-color: rgba(0, 0, 0, 0)
)). This is what it looks like if you add some padding-top
to the textarea (the highlight doesn't have an opportunity to update):
Facebook's approach is actually really clever, as you type in the text area the contents are evaluated and sent to a background element that looks like this:
<span class="highlighterContent"><b>#html</b> This is </span>
The <b>
element has the following css:
.uiMentionsInput .highlighter b {
background: linear-gradient(#DCE6F8, #BDCFF1) repeat scroll 0 0 rgba(0, 0, 0, 0);
border-radius: 2px;
box-shadow: 0 0 0 1px #A3BCEA;
font-weight: normal;
}
The smart thing is the span behind the text area has a transparent font so it takes the same space as the opaque font of the text area (allowing the <b>
highlight to stay aligned with the textarea text). Smart guys over there :)
you can make something like this:
http://jsfiddle/HR5N8/
<div id="something" contentEditable="true">
Hi, my name is <span class="name">John</span> and i'm 20 years old!
</div>
#something{
width: 400px;
height: 100px;
border: 1px solid grey;
}
.name{
background: #E1EEF5;
}
本文标签: javascriptFacebook Hashtags (Highlighting inside the textarea)Stack Overflow
版权声明:本文标题:javascript - Facebook Hashtags (Highlighting inside the textarea) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741907179a2404216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论