2016-06-11 198 views
0

我想做一个功能,可以让我切换元素的eventListener。切换事件监听器

在下面的例子中,我有三个按钮:关闭。当我点击按钮时,按钮变为有效。当我点击off按钮,按钮不应该工作了(但现在它仍然)。

现在我可以通过点击在第二次按钮实现期望的行为,但我想这是一个糟糕的巧合,它不应该是这样的。

也许我应该补充一点,我想不使用jQuery或类似的工具,它需要是一个函数,因为我打算将它用于很多按钮。

(我怀疑事情与范围引起该问题(clickHandler事件调用函数当激活按钮是不一样的clickHandler事件调用函数禁用的按钮)时,但我不能想办法对其进行测试。)

// buttons definitions, not important 
 
var mainButton = document.querySelector("#mainButton"); 
 
var onButton = document.querySelector("#onButton"); 
 
var offButton = document.querySelector("#offButton"); 
 

 
// main function 
 
var toggleButtons = function(toggleVal, button, element) { 
 
    var activateButton, clickHandler, disableButton; 
 

 
    // callback function for listener bellow 
 
    clickHandler = function() { 
 
     document.querySelector(element).classList.toggle("yellow"); 
 
    }; 
 

 
    activateButton = function() { 
 
     button.addEventListener("click", clickHandler); 
 
    }; 
 
    disableButton = function() { 
 
     button.removeEventListener("click", clickHandler); 
 
    }; 
 

 
    // when first argument is 1, make the button functional, otherwise disable its functionality 
 
    if (toggleVal === 1) { 
 
     activateButton(); 
 
    } else { 
 
     disableButton(); 
 
    } 
 
}; 
 

 
// when onButton is clicked, call main function with arguments 
 
// this works 
 
onButton.addEventListener("click", function() { 
 
    toggleButtons(1, mainButton, "body"); 
 
}); 
 

 
// this fails to disable the button 
 
offButton.addEventListener("click", function() { 
 
    toggleButtons(0, mainButton); 
 
});
.yellow { 
 
    background-color: yellow; 
 
}
<button type="button" id="mainButton">mainButton 
 
</button> 
 
<button type="button" id="onButton">onButton 
 
</button> 
 
<button type="button" id="offButton">offButton 
 
</button> 
 
<p>mainButton: toggles background color on click 
 
</p> 
 
<p>onButton: turns on mainButtons's functionality</p> 
 
<p>offButton: supposed to turn off mainButton's functionality</p>

+0

你有一个错字'offButon'。 – gcampbell

+0

(PS你可以用'true'和'false'作为'toggleVal'。) – gcampbell

+0

谢谢,错字是(不幸的)不是代码不工作的原因:(真/假当然是一个选项,它对我来说只是语义上的,1 = on,0 = off – lukas

回答

0
var mainButton = document.querySelector("#mainButton"); 
var onButton = document.querySelector("#onButton"); 
var offButon = document.querySelector("#offButton"); 

var element; // declare the element here and change it from toggleButtons when needed. 

function clickHandler() { 
    document.querySelector(element).classList.toggle('yellow'); 
} 
function activateButton(button) { // You missed this part 
    button.addEventListener('click', clickHandler); 
} 
function disableButton(button) { // You missed this part 
    button.removeEventListener('click', clickHandler); 
} 

function toggleButtons(value, button) {  
    if (value === 1) { 
     activateButton(button); // You missed this part 
    } else { 
     disableButton(button); // You missed this part 
    } 
}; 

onButton.addEventListener('click', function() { 
    element = 'body'; // you can change it to some other element 
    toggleButtons(1, mainButton); 
}); 
offButton.addEventListener('click', function() { 
    element = 'body'; // you can change it to some other element 
    toggleButtons(0, mainButton); 
}); 
+0

好的,我更新了我的答案,你没有传递引用'mainButton'这就是为什么它不起作用 – Azamantes

+0

谢谢!有没有可能提供参数给clickHandler?那么这将是绝对完美的! – lukas

+0

我更新了JS的问题,所以它更好地说明了我试图实现的目标。如果我需要将toggleButtons的值传递给clickHandler,那么我不能... – lukas