[英]JavaScript - Change text color
I tried to change text color via html select option. 我试图通过html select选项更改文本颜色。 I suppose it doesn't go this way I did it.
我想这不是我做的那样。 I'm new to this, so sorry in advance for some rookie mistakes.
我对此并不陌生,所以请您为一些菜鸟的错误提前感到抱歉。 Here's the code.
这是代码。
<!DOCTYPE html>
<html>
<body>
Select color:
<select id="mySelect">
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
</select>
<p>Click the button to change color of the text.</p>
<p id="par">Text color</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementById("mySelect").selectedIndex;
document.getElementById("par").style.color = x;
}
</script>
</body>
</html>
You need to get value from select: 您需要从select中获取价值:
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("par").style.color = x;
}
</script>
Try the below 试试下面
<!DOCTYPE html>
<html>
<body>
Select color:
<select id="mySelect">
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
</select>
<p>Click the button to change color of the text.</p>
<p id="par">Text color</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value
console.log(x)
document.getElementById("par").style.color = x;
}
</script>
</body>
</html>
Use x.options[x.selectedIndex].value
使用
x.options[x.selectedIndex].value
function myFunction() { var x = document.getElementById("mySelect") var color = x.options[x.selectedIndex].value document.getElementById("par").style.color = color; }
<!DOCTYPE html> <html> <body> Select color: <select id="mySelect"> <option value="black">Black</option> <option value="blue">Blue</option> <option value="red">Red</option> <option value="orange">Orange</option> </select> <p>Click the button to change color of the text.</p> <p id="par">Text color</p> <button type="button" onclick="myFunction()">Try it</button> </body> </html>
dynamic changing 动态变化
<!DOCTYPE html>
<html>
<body>
Select color:
<select id="mySelect" onChange="changeColor(value);">
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
</select>
<p>Click the button to change color of the text.</p>
<p id="par">Text color</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function changeColor(color) {
var x = document.getElementById("mySelect").selectedIndex;
document.getElementById("par").style.color = color;
}
</script>
</body>
</html>
On submit 提交时
<!DOCTYPE html>
<html>
<body>
Select color:
<select id="mySelect">
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
<option value="orange">Orange</option>
</select>
<p>Click the button to change color of the text.</p>
<p id="par">Text color</p>
<button type="button" onclick="changeColor()">Try it</button>
<script>
function changeColor() {
var x = document.getElementById("mySelect").value
document.getElementById("par").style.color = x;
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.