我在标题中有一个事件监听器:
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
}
};
降低我有一个功能:
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;
我的问题是: 我如何从事件监听器“执行”onRight变量?
答案 0 :(得分:0)
允许onRight
成为全局变量。虽然这并不总是令人满意,但在这种情况下它会起作用。请注意,it is best not to pollute the global namespace。
window.onRight = function showNext(img_thumb) {...
然后你可以用同样的方式访问它
arrow_right.onclick = window.onRight;