简体   繁体   English

如何从事件监听器访问函数。 JavaScript的

[英]How to access function from event listener. JavaScript

I have an event listener in header: 我在标题中有一个事件监听器:

window.onkeydown = function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    if(key == 27) {
        var panel = document.getElementById('largeImgPanel');
    hideMe(panel);
    }
    if(key == 39) {
        arrow_right.onclick = onRight; //Wrong
    }
};

Lower i have a function: 下层我有一个功能:

window.onload = function() {
           ...
    var onRight = function showNext(img_thumb) {
    index = index + 1;
    document.getElementById('largeImg').src = arr_big[index].src;
    showLargeImagePanel();
    unselectAll();
};
arrow_right.onclick = onRight;

My question is: How i can "execute" onRight variable from event listener? 我的问题是:如何从事件侦听器“执行” onRight变量?

Allow onRight to be a global variable. 允许onRight为全局变量。 Although this is not always desirable, in this instance it will work. 尽管这并不总是理想的,但在这种情况下它将起作用。 Keep in mind, it is best not to pollute the global namespace . 请记住, 最好不要污染全局名称空间

window.onRight = function showNext(img_thumb) {...

and then later you may access it the same way 然后您可以用相同的方式访问它

arrow_right.onclick = window.onRight;

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

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