Javascript 为什么上下文没有传递给eventlistener
目前,我正在做一个简单的应用程序,我正在访问dom并添加一些EventListener,问题是,我有一个类应用在3个按钮中,我想向其中3个按钮添加事件侦听器,但我想重用一个函数来显示和隐藏一个模式 基本上,当我在应用类的情况下单击其中一个按钮时,我希望显示模态 所以我做了这个:Javascript 为什么上下文没有传递给eventlistener,javascript,html,dom,Javascript,Html,Dom,目前,我正在做一个简单的应用程序,我正在访问dom并添加一些EventListener,问题是,我有一个类应用在3个按钮中,我想向其中3个按钮添加事件侦听器,但我想重用一个函数来显示和隐藏一个模式 基本上,当我在应用类的情况下单击其中一个按钮时,我希望显示模态 所以我做了这个: var showBackdrop = document.querySelector('.backdrop'); var showModal = document.querySelector('.modal'); var
var showBackdrop = document.querySelector('.backdrop');
var showModal = document.querySelector('.modal');
var confirmationNo = document.querySelector('.modal__confirmation--no')
var planChoices = document.querySelectorAll('.plan__choice');
function showModal() {
showBackdrop.style.display = 'block';
showModal.style.display = 'block';
}
function hideModal() {
showBackdrop.style.display = 'none';
showModal.style.display = 'none';
}
var ctx = this;
for(planChoice of planChoices) {
console.log(ctx);
planChoice.addEventListener('click', function() {
});
}
confirmationNo.addEventListener('click',function() {
hideModal();
})
问题是它找不到showModal函数,错误显示,showModal不是函数,为什么S您在同一执行上下文中对函数和变量使用了相同的名称
showmodel
,这可能会导致问题。showmodel是否也被定义为变量?请尝试更改变量或函数的名称。你是对的,在其他困难的事情中我忘记了简单的:D,谢谢