我试图将文本框中的颜色从蓝色更改为红色,但是它总是红色,事件侦听器由于某种原因而不工作。在脚本不工作中添加点击事件侦听器
function formFun() {
var textBox1 = document.forms[0].elements[0];
var textBox2 = document.forms[0].elements[1];
var button1 = document.forms[0].elements[2];
var button2 = document.forms[0].elements[3];
textBox2.style.backgroundColor = "blue";
button1.style.backgroundColor = "blue";
button2.style.backgroundColor = "blue";
button1.addEventListener("click", changeColor());
function changeColor() {
textBox1.style.backgroundColor = "red";
}
}
<form name="mForm">
<input type="text" name="in1">
<input type="text" name="in2">
<button name="butt1">click1</button>
<button name="butt2">click2</button>
</form>
如果你可以创建一个可运行的代码,比如在jsfiddle等等 – EntryLeveDeveloper