[英]My EventListener to a button continues to fire despite ownership being changed, why? (JavaScript)
I have a function, which at the end of its task assigns a button to a new id. 我有一个函数,在其任务结束时为新id分配一个按钮。
function altChecker() {
var doc = document,
out = [],
appButton = doc.getElementById('appButton'),
//re = /click-me/gi,
output = doc.createElement('p');
output.setAttribute('id', 'output');
EventUtility.addHandler(appButton, 'click', function(e) {
//I have not included all the function details to concentrate on the question
appButton.id = 'appButtonNextChecker';
var appButtonNextChecker = doc.getElementById('appButtonNextChecker');
nextChecker(appButtonNextChecker);
});
}
function nextChecker(newBtnName) {
EventUtility.addHandler(newBtnName, 'click', function(e) {
$('#output').innerHTML = "";
console.log('next Checker, button!')
});
}
So basically there is one button in the DOM assigned to appButton
ID initially, and then I change it doing: 所以基本上DOM中有一个按钮分配给
appButton
ID,然后我改变它:
appButton.id = 'appButtonNextChecker';
when the altChecker
function fires... 当
altChecker
函数触发时......
Then I assign the button to a new variable, and pass in the variable to the next function... 然后我将按钮分配给一个新变量,并将变量传递给下一个函数......
var appButtonNextChecker = doc.getElementById('appButtonNextChecker');
nextChecker(appButtonNextChecker);
While I can see the buttons' ID change in the DOM, and I see the console.log
fire in the nextChecker
function, 虽然我可以在DOM中看到按钮的ID更改,但我在
nextChecker
函数中看到console.log
触发,
$('#output').innerHTML = ""; //doesn't fire
AND the altChecker
function fires as well (again)?! 并且
altChecker
函数altChecker
激活(再次)?! Haven't I severed the connection to the click
function when I reassigned the new ID? 当我重新分配新ID时,我没有切断与
click
功能的连接吗?
Any help would be appreciated! 任何帮助,将不胜感激!
Javascript doesn't remember that you initially attached the event through it's id. Javascript不记得您最初通过它的id附加了事件。 The event is attached to the element itself, not the ID.
事件附加到元素本身,而不是ID。 It's not like CSS that way.
它不像CSS那样。
In fact your variables are still holding the same element as well, so there's no need to create a new variable after changing the ID, either. 实际上,您的变量仍然保持相同的元素,因此在更改ID之后也不需要创建新变量。 Since you're using jQuery you can just type
$(appButton).unbind();
由于您使用的是jQuery,因此只需键入
$(appButton).unbind();
to remove the event handler. 删除事件处理程序。 You may also want to look into
.on()
and .off()
您可能还想查看
.on()
和.on()
.off()
The problem is that you're trying to use the innerHTML
property in a jQuery's object. 问题是你试图在jQuery的对象中使用
innerHTML
属性。
That property belongs to Element
, and it will not work in the way you're using it. 该属性属于
Element
,它不会以您使用它的方式工作。
You can use the document.getElementById
method, and it will work fine: 您可以使用
document.getElementById
方法,它可以正常工作:
document.getElementById('output').innerHTML = '';
Or you can use jQuery's html
method: 或者你可以使用jQuery的
html
方法:
$('#output').html('');
And you can even use the first element of the jQuery's array, and use innerHTML
again: 你甚至可以使用jQuery数组的第一个元素,并再次使用
innerHTML
:
$('#output')[0].innerHTML = '';
It's up to you, but the first option will be faster, for sure. 这取决于你,但第一个选择肯定会更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.