我在代码中间添加了注释。请帮帮我如果可以,我今天必须交付产品。
if (a === 3) { //this would be how many of the answers further the action
var starting = 2; // for an example: yes or no; two of the answers go to no, one goes to yes.
// we take 2 as a starting point /you can set these individually for each scenario;
if (starting === 2) {
for (i = 0; i < starting; i++) {
answers[i] = 1; //sets the two checking points of the answer to the no, remaining yes;
document.getElementById("btn" + i).style.backgroundColor = "red";
// the problem lies here
// I tried multiple ways but none of them worked so far
//i want to apply the style change to multiple buttons at once.
}
alert(answers);
for (i = 0; i < starting; i++) {
if (answers[i] == 1) {
}
}
}
}
问题是按钮的名称是btn,按钮的名称是btn1,btn2,btn3;我希望for循环将其更改为所有按钮,但是id的字符串文字不能识别i;
示例:“btn”+ i; =改变btn1的风格
我修好了。错误是for循环从1开始;而且我已经将按钮的ID命名为错误。新手错误! :)
for (i = 0; i < 3; i++) {
if (document.getElementById("btn" + i) != undefined)
document.getElementById("btn" + i).style.backgroundColor = "red";
}
<input type="button" value="Submit" id="btn3">
<input type="button" value="Submit" id="btn1">
<input type="button" value="Submit" id="btn2">
问题是你的按钮id从btn1开始,你的代码从0开始,它不在你的html中,所以要么在设置背景颜色之前添加一个检查,要么从1开始你的循环
if (document.getElementById("btn" + i) != undefined)