按钮不会停止关闭

时间:2016-01-25 15:04:10

标签: javascript html

我进行了此设置,因此当提示按钮为“ON”时,当您开始拖动项目时,目标将突出显示。这很好用。但是当我开始拖动项目时,我将按钮切换为“OFF”,按钮返回到“ON”位置并高亮显示目标区域。按钮如何保持“关闭”以便不突出显示? 这里是按钮代码(小提琴中的完整代码)

function onoff() {
currentvalue = document.getElementById('onoff').value;
if (currentvalue == "Off") {
  document.getElementById("onoff").value = "On";
} else {
  document.getElementById("onoff").value = "Off";
}
}

这里是jsfiddle Button won't stay OFF

1 个答案:

答案 0 :(得分:0)

为了达到目的,在你的小提琴中,函数ondragstart有一堆条件做分配,例如:document.getElementById("onoff").value="On",为了比较值,你应该使用== ,并且为了标记值和类型,您应该使用===

下面是ondragstart函数中应该如何:

if((document.getElementById("onoff").value === "On") && (data === "item1" || data === "item2" || data === "item3" || data === "item4")){
  (document.getElementById("fruit").style.color = "red") && (document.getElementById("veg").style.color = "black") && (document.getElementById("games").style.color = "black");
}
else if((document.getElementById("onoff").value === "On") && (data === "item5" || data === "item6" || data === "item7" || data === "item8")){
  (document.getElementById("veg").style.color = "red") && (document.getElementById("fruit").style.color = "black") && (document.getElementById("games").style.color = "black");
}
else if((document.getElementById("onoff").value === "On") && (data === "item9" || data === "item10" || data === "item11" || data === "item12")){
  (document.getElementById("games").style.color = "red") && (document.getElementById("fruit").style.color = "black") && (document.getElementById("veg").style.color = "black");
}
else if((document.getElementById("onoff").value === "Off") &&  (data === "item1" || data === "item2" || data === "item3" || data === "item4" || data === "item5" || data === "item6" || data === "item7" || data === "item8" || data === "item9" || data === "item10" || data === "item11" || data === "item12")){
  (document.getElementById("fruit").style.color = "black") && (document.getElementById("veg").style.color = "black") && (document.getElementById("games").style.color = "black");
}