2016-01-22 126 views
0

我需要更改textarea中選定文本的文本顏色,當我單擊按鈕時。更改選定文本的顏色Javascript

我第一次嘗試:

document.getSelectedText(document.getElementById('test22')).style.color = '#0F0'; 

我謝勝利嘗試:

document.getSelectedText((test22.value).substring(test22.selectionStart, test22.selectionEnd)).style.color = '#0F0'; 

HTML代碼:

<textarea name="area1" cols="40" id="test22"></textarea> 
<button onclick="test();">CLICK</button> 

謝謝! ;)

+0

Problably這個副本:http://stackoverflow.com/questions/17288964/how-to-change-color-of-the-selected-文本動態地按一下按鈕 – Mijago

+0

是的,但我需要這個texarea。這隻適用於「正常」文本。 – Tuxio

+0

對不起:)我會再看看來幫助你:) – Mijago

回答

0

有沒有辦法在文字區域進行此設置,因爲它們不是彪是RTF編輯器。

可以使用CONTENTEDITABLE =「true」標記與股利和做魔術那裏,就像在這裏和那裏設置「字體」標籤。

您也可以使用類似https://www.tinymce.comhttp://ckeditor.com。這兩個都是所見即所得編輯器。 或在github上搜索任何輕型的RTF編輯器。

也許你可以從中得到一些想法:http://dipaksblogonline.blogspot.in/2014/11/javascript-text-selection-popover.html