我试图用布尔值设置一个变量(currentState),但在条件变量中似乎无法识别该变量。我遗漏了什么?
function toggle(customID) { // Function set on button, 'customID' is the ID
// of element to toggle with button. Ex:
// <button onclick="(toggle('menu')">Button</button>
var theToggledElement = document.getElementById(customID);
var currentState = false; // false = hidden. true = visible.
if (currentState === false) {
currentState = true;
theToggledElement.style.left = '0px';
console.log('State set to "visible" and left to "0px"');
}
else if (currentState === true) {
currentState = false;
theToggledElement.style.left = 'calc(-100% + (0.5cm + 1.461544602cm + .5cm))';
console.log('State set to "hidden" and left to "calc(-100% + (0.5cm + 1.461544602cm + .5cm))"')
}
}
发布于 2018-06-23 06:48:00
@Barmar谢谢!将"currentState“变量移出,如下所示:
var currentState = false; // false = hidden. true = visible.
function toggle(customID) { // Function set on button, 'customID' is the ID
// of element to toggle with button. Ex:
// <button onclick="(toggle('menu')">Button</button>
var theToggledElement = document.getElementById(customID);
if (currentState === false) {
currentState = true;
theToggledElement.style.left = '0px';
console.log('State set to "visible" and left to "0px"');
}
else if (currentState === true) {
currentState = false;
theToggledElement.style.left = 'calc(-100% + (0.5cm + 1.461544602cm + .5cm))';
console.log('State set to "hidden" and left to "calc(-100% + (0.5cm + 1.461544602cm + .5cm))"')
}
}
https://stackoverflow.com/questions/50996260
复制相似问题