简体   繁体   English

FireFox Javascript事件处理

[英]FireFox Javascript event handling

I have the following function that works in Chrome/IE 我有以下功能可在Chrome / IE中使用

for(var i = 0; i < 5; ++i) {
        document.all["elems" + i].onscroll = getFunc("onScrollAdj(document.all['tb" + i + "']);");
    }

function getFunc(jsString) {
    return new Function(jsString);
}

However I get ReferenceError: event is undefined . 但是我得到ReferenceError: event is undefined

I have tried to re-write the function to include an event however I then get another error var i is undefined . 我试图重新编写函数以包含一个事件,但是随后又遇到另一个错误var i is undefined

  document.all["elems" + i].onscroll = onScrollAdj(event, document.all['tb" + i + "');

Is there any way to ensure both event and attributes can be passed? 有什么方法可以确保事件和属性都可以传递?

however I get 'ReferenceError: event' is undefined. 但是我得到'ReferenceError:event'未定义。

That's because you're trying to use event without declaring it as an argument. 那是因为您尝试使用event而不将其声明为参数。 That only works on Microsoft browsers, which make event a global, and Chrome which throws a bone to Microsoft-only code. 这仅在使event成为全局event的Microsoft浏览器上起作用,而在Chrome浏览器中仅适用于仅Microsoft的代码起作用。 You will need to declare the argument. 您将需要声明参数。

There's no need for new Function virtually ever, and certainly not in this case: 实际上,几乎不需要new Function ,在这种情况下当然不需要:

for(var i = 0; i < 5; ++i) {
    document.all["elems" + i].onscroll = getFunc(i);
    // ------------------------------------------^
}

function getFunc(i) {
// --------------^
    return function(event) { onScrollAdj(event, document.all['tb' + i]); };
    // -------------^--------------------^
}

Note that you'll have to ensure that onScrollAdj accepts event : 请注意,您必须确保onScrollAdj接受event

function onScrollAdj(event, tbThingy) {
// ------------------^

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

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