为什么在上游有事件时没有定义“事件”变量? [英] Why is there an 'event' variable available without being defined when there was an event upstream?

查看:266
本文介绍了为什么在上游有事件时没有定义“事件”变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我偶然发现了一个奇怪的行为。基本上,我有一个绑定到knockout.js单击事件的函数。

  this.myClickHandler = function()这个函数使用了knockout事件,但没有明确地将其作为参数。 ){
console.log(event); //事件被登录到Chrome / IE11中,而不是Firefox
}

这看起来很奇怪我,但在Chrome中,它按预期工作。它也在IE11中正常工作。然而,在Firefox中,它没有起作用。只要我明确表示它在所有浏览器中都有效。这是我所期望的所有工作都需要的。

  this.myClickHandler = function(model,event) {//事件是从敲除单击事件传递的第二个参数
console.log(event); //在所有浏览器中记录事件
}

使用jQuery以及

function func(){alert(event); } function runFunc(callback){callback(); } $(document).ready(function(){runFunc(func);});

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>  



那么这是如何工作的?为什么我可以在某些浏览器中访问一个名为'event'的变量,而不是其他的浏览器?这是预期的行为?

解决方案

这是从旧版本的IE获得的东西,事件对象没有传递给处理程序方法它是在全球范围内设置的。

为了向后兼容,IE仍然支持这种模式,而chrome也增加了对此功能的支持。但是FF不支持它。




I stumbled across an odd behaviour today. Basically, I had a function bound to a knockout.js click event. The function was making use of the knockout event, but was not explicitly taking it as an argument.

this.myClickHandler = function(){
    console.log(event); //event gets logged in Chrome/IE11, not Firefox
}

This looked weird to me, but in Chrome it was working as expected. It also worked correctly in IE11. In Firefox however, it did not function. As soon as I explicitly it worked in all browsers. This is what I would expect would be needed for it to work at all.

this.myClickHandler = function(model, event){ //event is second parameter passed from knockout click event
    console.log(event); //event gets logged in all browsers
}

I had a play around and reproduced this with jQuery as well

    function func(){
     alert(event);
    }
    
    function runFunc(callback){
      callback();
    }
    
    $(document).ready(function(){
      runFunc(func);
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So how is this working? Why do I have access to a variable called 'event' in some browsers but not others? Is this intended behaviour?

解决方案

It is something that got carried from the old version of IE, where the event object was not passed to the handler method instead it was set in the global context.

For backward compatibility IE still supports this model and chrome also has added support for this feature. But FF is not supporting it.

这篇关于为什么在上游有事件时没有定义“事件”变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆