我尝试使用下拉菜单更改输入文本的颜色,但它不起作用。请帮忙
function selectColor()
{
var color = document.getElementById('colours').value;
document.getElementById("text1").style.color = color;
}

<h3>Text Colour:
<select id="colours">
<option value="None" onclick = "selectColor()">
<option value="red" onclick = "selectColor()" >Red
<option value="green" onclick = "selectColor()">Green
<option value="blue" onclick = "selectColor()">Blue
<option value="pink" onclick = "selectColor()">Pink
</select>
</h3>
<p id="text1">Text color</p>
&#13;
答案 0 :(得分:3)
三件事:
onchange
代码select
事件
text1
id,span,div,paragraph,您的选择添加标记。
<script>
function selectColor()
{
var color = document.getElementById('colours').value;
document.getElementById("text1").style.color = color;
}
</script>
<h3>Text Colour:
<select id="colours" onchange="selectColor();">
<option value="None"></option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="pink">Pink</option>
</select>
</h3>
<span id="text1">Color selected!</span>
&#13;
答案 1 :(得分:1)
现在看,我是一个不引人注目的javascript粉丝。如果可能的话,我将从HTML中分离出脚本。下面是H3和一个select,脚本本身定义了事件处理程序。
document.getElementById("colours").onchange = selectColor;
function selectColor()
{
var color = document.getElementById('colours').value;
document.getElementById("text1").style.color = color;
}
&#13;
<h3 id="text1">Text Colour: </h3>
<select id="colours">
<option value="None">
<option value="red" >Red
<option value="green">Green
<option value="blue" >Blue
<option value="pink" >Pink
</select>
&#13;
答案 2 :(得分:0)
您的代码有多个问题有些人在另一个答案中说。
1.关闭您的选项标签。
2.设置第一个选项禁用(它更好)
使用此
function selectColor()
{
var color = document.getElementById('colours').value;
document.getElementById("text1").style.color = color;
}
&#13;
<h3 id="text1">Text Colour:</h3>
<select id="colours" onchange="selectColor()">
<option value="None" disabled="disabled" >Select</option>
<option value="red" >Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="pink">Pink</option>
</select>
&#13;
答案 3 :(得分:0)
您需要使用&#34; setAttribute&#34; :)
function selectColor()
{
var color = document.getElementById('colours').value;
document.getElementById("text1").setAttribute('style', 'color:'+color);
}
&#13;
<h3>Text Colour:
<select id="colours" onchange="selectColor();">
<option value="None" >
<option value="red" >Red
<option value="green" ">Green
<option value="blue" ">Blue
<option value="pink" ">Pink
</select>
</h3>
<span id="text1">Color selected!</span>
&#13;