<tfoot id='ebjzV'></tfoot>

    <small id='ebjzV'></small><noframes id='ebjzV'>

    <legend id='ebjzV'><style id='ebjzV'><dir id='ebjzV'><q id='ebjzV'></q></dir></style></legend>
    <i id='ebjzV'><tr id='ebjzV'><dt id='ebjzV'><q id='ebjzV'><span id='ebjzV'><b id='ebjzV'><form id='ebjzV'><ins id='ebjzV'></ins><ul id='ebjzV'></ul><sub id='ebjzV'></sub></form><legend id='ebjzV'></legend><bdo id='ebjzV'><pre id='ebjzV'><center id='ebjzV'></center></pre></bdo></b><th id='ebjzV'></th></span></q></dt></tr></i><div id='ebjzV'><tfoot id='ebjzV'></tfoot><dl id='ebjzV'><fieldset id='ebjzV'></fieldset></dl></div>

        <bdo id='ebjzV'></bdo><ul id='ebjzV'></ul>
    1. 如何在使用类方法作为回调的类中添加事件处理

      时间:2023-09-05
      <tfoot id='0lRLL'></tfoot>

        <legend id='0lRLL'><style id='0lRLL'><dir id='0lRLL'><q id='0lRLL'></q></dir></style></legend>
        <i id='0lRLL'><tr id='0lRLL'><dt id='0lRLL'><q id='0lRLL'><span id='0lRLL'><b id='0lRLL'><form id='0lRLL'><ins id='0lRLL'></ins><ul id='0lRLL'></ul><sub id='0lRLL'></sub></form><legend id='0lRLL'></legend><bdo id='0lRLL'><pre id='0lRLL'><center id='0lRLL'></center></pre></bdo></b><th id='0lRLL'></th></span></q></dt></tr></i><div id='0lRLL'><tfoot id='0lRLL'></tfoot><dl id='0lRLL'><fieldset id='0lRLL'></fieldset></dl></div>

          <tbody id='0lRLL'></tbody>
        • <bdo id='0lRLL'></bdo><ul id='0lRLL'></ul>

            <small id='0lRLL'></small><noframes id='0lRLL'>

              • 本文介绍了如何在使用类方法作为回调的类中添加事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何在一个类中添加一个事件处理程序,并将类方法作为回调?

                How do I add an event handler inside a class with a class-method as the callback?

                <div id="test">move over here</div>
                <script>
                    oClass = new CClass();
                    function CClass()
                    {
                        this.m_s = "hello :-/";
                        this.OnEvent = OnEvent;
                        with(this)
                        {
                            var r = document.getElementById("test");
                            r.addEventListener('mouseover', this.OnEvent);  // this does NOT work :-/
                        }
                      
                        function OnEvent()
                        {
                            alert(this);    // this will be the HTML div-element
                            alert(this.m_s);    // will be undefined :-()
                        }
                    }
                </script>
                

                是的,我知道一些让它工作的怪癖,但是当这些事件处理程序被引入时,预期的方式是什么???我再次有一种苦涩的感觉,没有人真正生活在 OOP 中:-(

                Yes I know some quirks to make it work but what would be the intended way when these event handlers were introduced ??? I again have the bitter feeling, that no-one truly lives OOP :-(

                这里给你玩:https://jsfiddle.net/sepfsvyo/1/

                推荐答案

                事件监听回调中的 this 将是触发事件的元素.如果您希望 this 成为您的类的实例,那么:

                The this inside the event listener callback will be the element that fired the event. If you want the this to be the instance of your class, then either:

                将函数绑定到类实例:

                使用 Function.prototype.bind,将创建​​一个新函数,它的 this 值将始终是您指定的值(类实例):

                Using Function.prototype.bind, will create a new function that its this value will always be what you specify it to be (the class instance):

                r.addEventListener('mouseover', this.OnEvent.bind(this));
                //                                          ^^^^^^^^^^^
                

                将函数包装在匿名函数中:

                var that = this;
                r.addEventListener('mouseover', function(ev) { that.OnEvent(ev); });
                //                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                

                或使用箭头函数(所以不需要 that):

                or use an arrow function (so no need for that):

                r.addEventListener('mouseover', ev => this.OnEvent(ev));
                //                              ^^^^^^^^^^^^^^^^^^^^^^
                

                注意:正如下面的评论中提到的,上述两种方法都将不同的函数传递给 addEventListener(带有 bind 的函数创建一个新函数,而异常函数显然是 !== this.OnEvent).如果您稍后要删除事件侦听器,则必须存储对该函数的引用:

                Note: As mentioned in a comment bellow, both of the above methods pass a different function to addEventListener (the one with bind create a new function, and the anounimous function is obviously !== this.OnEvent). If you are going to remove the event listener later, you'll have to store a reference to the function:

                var reference;
                r.addEventListener('mouseover', reference = this.OnEvent.bind(this));
                //                              ^^^^^^^^^^^^
                

                或:

                var reference;
                var that = this;
                r.addEventListener('mouseover', reference = function(ev) { that.OnEvent(ev); });
                //                              ^^^^^^^^^^^^
                

                然后你可以像这样删除事件监听器:

                then you can remove the event listener like:

                r.removeEventListener('mouseover', reference);
                

                这篇关于如何在使用类方法作为回调的类中添加事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:jQuery: $().click(fn) 与 $().bind('click',fn); 下一篇:假“点击"激活 onclick 方法

                相关文章

                最新文章

                1. <i id='EE9sx'><tr id='EE9sx'><dt id='EE9sx'><q id='EE9sx'><span id='EE9sx'><b id='EE9sx'><form id='EE9sx'><ins id='EE9sx'></ins><ul id='EE9sx'></ul><sub id='EE9sx'></sub></form><legend id='EE9sx'></legend><bdo id='EE9sx'><pre id='EE9sx'><center id='EE9sx'></center></pre></bdo></b><th id='EE9sx'></th></span></q></dt></tr></i><div id='EE9sx'><tfoot id='EE9sx'></tfoot><dl id='EE9sx'><fieldset id='EE9sx'></fieldset></dl></div>

                    <tfoot id='EE9sx'></tfoot>

                  1. <small id='EE9sx'></small><noframes id='EE9sx'>

                      <bdo id='EE9sx'></bdo><ul id='EE9sx'></ul>
                  2. <legend id='EE9sx'><style id='EE9sx'><dir id='EE9sx'><q id='EE9sx'></q></dir></style></legend>