javascript - Styling letters of a word within an input differently, possible? - Stack Overflow

Wondering if this is even possible, but if I have a input containing text, for example lets say the wor

Wondering if this is even possible, but if I have a input containing text, for example lets say the word 'Test' is in the input. What I would like to be able to do is change the styling on the individual letters of the word 'Test'

I would like the 'Te' to be bold and then have the 'st' be regular.

It wouldn't have to be bold, maybe I would like the 'e' to be red or something like that.

Any ideas on how this might be acplished?

Wondering if this is even possible, but if I have a input containing text, for example lets say the word 'Test' is in the input. What I would like to be able to do is change the styling on the individual letters of the word 'Test'

I would like the 'Te' to be bold and then have the 'st' be regular.

It wouldn't have to be bold, maybe I would like the 'e' to be red or something like that.

Any ideas on how this might be acplished?

Share Improve this question edited May 23, 2011 at 18:14 Mike Fielden 10.2k14 gold badges60 silver badges100 bronze badges asked May 23, 2011 at 18:04 Collin EstesCollin Estes 5,8097 gold badges53 silver badges71 bronze badges 1
  • I know of no way to get a browser to style text character-by-character in an <input> field, but I'll take this opportunity to plug Lettering.js :-) (It won't solve the problem, unfortunately.) – Pointy Commented May 23, 2011 at 18:12
Add a ment  | 

4 Answers 4

Reset to default 5

Don't think it is possible (will do some more test).

What about adding a contenteditable div which looks like a input?

Simple contenteditable exmaple: http://jsfiddle/PpEx7/

EDIT

Nopez not possible. :)

You should take a look at how HTML WYSIWYG editors are build.

Basically, they

  • hide the input field and display another html element with the styled content: Highlight text as you type on textarea

or

  • use design mode in html: javascript Rich Text Editors

both ways are not trivial...

If you take a look at the MDN CSS Reference you can see for yourself that there is no selector for single letters inside a field.

The best you can do is use :first-letter

But As you can see it does not work on <input />

No it's not possible with an <input type="text"> tag. You can however trick the user into believing he is using a styled input by replacing it with a contendeditable div or something.

However, I looked into this couple of years ago, and it's a mess if you want a reliable cross browser solution for this. Unless I remembered wrong the html from just showing a bold text in a contenteditable div could easily result in the following across the major browsers:

<BOLD style="font-weight:bold">Bold</BOLD> <!-- IE -->
<span style="font-weight:bold">Bold</span> <!-- Chrome -->
<b>Bold</b> <!-- Firefox -->
<strong>Bold</strong> <!-- Safari -->
<lol style="fOnt-WEIGHT: bold;">Bold</lol> <!-- IE6 -->

No kiddin.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744300039a4567455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信