2011-03-27 42 views
3

我只想知道如何將textarea的背景顏色更改爲在文本框中鍵入的顏色。我已經設法做文本的顏色,字體和大小,但以相同的方式做背景似乎不起作用。 我的文本顏色和背景代碼:如何使用JavaScript更改textarea的背景顏色

腳本:

function setColor(where, Color) 
    { 
     if (where == "backgroundcolour") 
      document.getElementById('textarea').style.backgroundColor = Color; 
     if (where == "colourtext") 
      document.getElementById('textarea').style.color = Color; 
    } 

HTML:

<p> 
    Card color: <input type = "text" name = "backgroundcolour" 
          size = "10" 
          onchange = "setColor('backgroundcolour', 
               this.value)"> 
    <br> 
    Text color: <input type = "text" name = "colourtext" 
          size = "10" 
          onchange = "setColor('colourtext', 
               this.value)"> 
    <br> 
</p> 
<textarea id = 'textarea' name="data" cols="100" rows="10"> 

</textarea> 

它好像我的互聯網擋住腳本,以便它wouldnt改變背景

+0

你應該使用jQuery做 – CarneyCode 2011-03-27 12:34:32

+0

文本是如何被輸入? – 2011-03-27 12:35:00

+0

我看到多數民衆贊成在工作 – 2011-03-27 12:36:42

回答

0

您的代碼實際上正在工作。也許你忘了從文本框中移除焦點,以便觸發更改事件。

function setColor(where, Color) 
 
    { 
 
     if (where == "backgroundcolour") 
 
      document.getElementById('textarea').style.backgroundColor = Color; 
 
     if (where == "colourtext") 
 
      document.getElementById('textarea').style.color = Color; 
 
    }
<p> 
 
    Card color: <input type = "text" name = "backgroundcolour" 
 
          size = "10" 
 
          onchange = "setColor('backgroundcolour', 
 
               this.value)"> 
 
    <br> 
 
    Text color: <input type = "text" name = "colourtext" 
 
          size = "10" 
 
          onchange = "setColor('colourtext', 
 
               this.value)"> 
 
    <br> 
 
</p> 
 
<textarea id = 'textarea' name="data" cols="100" rows="10"> 
 

 
</textarea>