在做項目時遇到需要通過鼠標觸發事件,然後在js函數中改變text文本域的顏色,下面是具體的操作,寫下來幫助記憶:
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>