我最近完成了一个关于创建交通灯的项目,该项目在HTML中单击按钮时循环,但是当我用文本列表尝试(编辑)它时它不起作用。目的是按顺序循环遍历人员列表,从列表中的第一个开始,但它不起作用!关于如何解决它的任何想法?
完整脚本:
<fieldset>
<legend>PRIORITY: ICT DEPT</legend>
<p id="demo"></p>
<button onclick="change()">></button>
</fieldset>
priority=0
var ictTeachers = ["Mrs. West", "Mr. Mulligan", "Mrs. Davidson","Mr. Rowan" "Mr. Demolder" "Mr. Schander" "Mr. Blackwell"];
document.getElementById("demo").innerHTML = ictTeachers[1];
function change() {
if(priority==0) {
document.getElementById("demo").src = ictTeachers[1]
position=1;
}else if(priority==1) {
document.getElementById("demo").src = ictTeachers[2]
position=2;
}else if(priority==2) {
document.getElementById("demo").src = ictTeachers[3]
position=3;
}else if(priority==3) {
document.getElementById("demo").src = ictTeachers[4]
position=4
}else if(priority==4) {
document.getElementById("demo").src = ictTeachers[5]
position=5;
}else if(priority==5) {
document.getElementById("demo").src = ictTeachers[6]
position=6;
}else if(priority==6) {
document.getElementById("demo").src = ictTeachers[0]
position=0;
}
}
答案 0 :(得分:2)
主要原因是,当您使用.src
或.innerHTML
.innerText
然而,你似乎使你的生活变得复杂。您应该尝试简化代码。
document.getElementById("demo").innerHTML = ictTeachers[1];
var position = 0;
function change() {
document.getElementById("demo").innerHTML = ictTeachers[position+1]
position=(position + 1) % ictTeachers.length;
}
您还应该确保没有语法错误。你忘记了阵列中的一些昏迷。
var ictTeachers = ["Mrs. West", "Mr. Mulligan", "Mrs. Davidson","Mr. Rowan", "Mr. Demolder", "Mr. Schander", "Mr. Blackwell"];
答案 1 :(得分:0)
您没有更改优先级变量
if(priority==0) {
document.getElementById("demo").src = ictTeachers[1]
position=1;
}
且priority
值仍为0,尝试将所有变量position
重命名为priority
答案 2 :(得分:0)