简体   繁体   中英

Changing background color when highligted

Can anyone help me? I have a textarea and three buttons with different colors.

I wanted to change the hightlighted text bgcolor when button click in javascript or css. Please help. I don't really done this, but I have a sample of CKEDITOR.

This the Sample of CKEDITOR:

在此处输入图片说明

This my code in textarea:

<textarea>
  <p> CHANGING BACKGROUND </p>
</textarea>

You can't do this in a textarea. You need an HTML Element with contentEditable .

And take a look to document.execCommand()

Also you can simulate a <textarea /> with CSS.

Everything is showed below:

 function setColor() { document.execCommand('backColor', false, "red"); } 
 div[contentEditable] { -moz-appearance: textfield-multiline; -webkit-appearance: textarea; border: 1px solid gray; font: medium -moz-fixed; font: -webkit-small-control; height: 28px; overflow: auto; padding: 2px; resize: both; width: 400px; } 
 <div contentEditable="true">this is some text</div> <button onclick="setColor();">change color</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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