简体   繁体   English

尽管所有权发生了变化,我的EventListener仍会继续触发,为什么? (JavaScript的)

[英]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.

相关问题 为什么我的eventListener从不触发? - why does my eventListener never fire? 为什么这个eventlistener没有在我的手机上启动设备? - Why doesn't this eventlistener for deviceready fire on my phone? 为什么第二个事件监听器不触发? - Why does the second eventlistener not fire? 为什么未添加eventListener? - Why is eventListener not being added? 光标继续显示-尽管将其设置为none,并且仅在div上显示。 Angular2 / JavaScript的 - Cursor continues to display - despite it being set to none, and only when over a div. Angular2/Javascript 使用JavaScript更改了TextListener值的EventListener吗? - EventListener for textarea value changed with JavaScript? 使用事件侦听器在 Javascript 中提交表单不会触发 get 请求 - Submitting a form in Javascript with an eventlistener will not fire get request javascript eventlistener立即触发,而不是在事件上触发 - javascript eventlistener fire immediately instead of on event 为什么在关闭我的Android应用程序后javascript会继续? - Why javascript continues after I close my android app? jQuery / Javascript-当按钮的值更改时,如何触发事件? - jQuery/Javascript - How to fire an event when a button's value is changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM