简体   繁体   English

向JavaScript中的预定义事件添加功能

[英]Adding functionality to a pre-defined event in JavaScript

Is there a way in JavaScript that I can override an event, but still call the original event's code? JavaScript中是否有一种方法可以覆盖事件,但仍可以调用原始事件的代码? I basically want to add a single line of code to the end. 我基本上想在最后添加一行代码。 So for example: 因此,例如:

Original function 原始功能

function DropDownList_OnFocus(a, b) {
    // Original Code
}

What I'd need 我需要什么

function DropDownList_OnFocus(a, b) {
    // Original Code
    // New Code        
}

Event handlers always execute sequentially in the order they were bound. 事件处理程序始终按绑定的顺序顺序执行。

So You can simply add another event handler for the same event on the same element and put more/remaining code in the second event handler. 因此,您可以简单地在同一元素上为同一事件添加另一个事件处理程序,然后将更多/剩余的代码放入第二个事件处理程序中。

I've managed to get it working using the following: 我设法使用以下方法使其工作:

var base = DropDownList.OnFocus;
var DropDownOnFocusOverride = (function () {
    return function () {
        base();
        // Additional Code
    }
});

DropDownList.OnFocus = DropDownOnFocusOverride();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM