javascript evt = (evt) 的含义?evt : window.event

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12635142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:44:02  来源:igfitidea点击:

Meaning of evt = (evt) ? evt : window.event

javascriptjavascript-events

提问by amesh

Hi what does this javascript snippet means.(evt) part is so confusing.. evt is not a boolean. How it works?

嗨,这个 javascript 代码段是什么意思。(evt) 部分是如此令人困惑.. evt 不是布尔值。怎么运行的?

function checkIt(evt) {
        evt = (evt) ? evt : window.event
        var charCode = (evt.which) ? evt.which : evt.keyCode

    }

回答by Blender

evt = (evt) ? evt : window.eventis just the inline ifsyntax. It's equivalent to this code:

evt = (evt) ? evt : window.event只是内联if语法。它等效于以下代码:

if (evt) {
    evt = evt;
} else {
    evt = window.event;
}

If evtis truthy, evtwill be left alone. If evtisn't truthy, it will be replaced with window.event.

如果evt是真实的,evt将被单独留下。如果evt不是真的,它将被替换为window.event.

回答by Norguard

It's for event listeners.

它用于事件侦听器。

IE6-IE8 used a totally different event method than the W3C standard.

IE6-IE8 使用了与 W3C 标准完全不同的事件方法。

When an event fires, a W3C-standard browser will pass an event object in the callback:

当事件触发时,W3C 标准浏览器将在回调中传递一个事件对象:

function keyPressed (e) { /* do stuff with e */ }

In your case, it's keydown(or something else using keyCode).
IE didn't support this, instead it had window.eventwhich was updated every time an event happened.

在您的情况下,它是keydown(或其他使用keyCode)。
IE 不支持这一点,而是在window.event每次事件发生时更新。

So your function is checking to see if an object was passed into it:

所以你的函数正在检查一个对象是否被传递给它:

evt = (evt) ? evt : window.event;
// does `evt` exist, and is it anything but '', 0, false, null, undefined, NaN
// yes: evt = itself (W3C)
// no: evt = window.event (IE6-8)

Then the code asks if evt.whichexists, to try to figure out where to get the keyCode from. evt.keyCodeis what you should be using for modern browsers, in the case of keydownand keyup.

然后代码询问是否evt.which存在,以尝试找出从何处获取 keyCode。 evt.keyCode是你应该使用现代浏览器的东西,在的情况下keydownkeyup

回答by jAndy

Assignment expressions like that are evaluated from right to left, so this means:

像这样的赋值表达式是从右到左计算的,所以这意味着:

  • if evthas a truthyvalue, assign this value back to evt
  • if not, assign the value of window.eventregardless of its content to evt
  • 如果evt有一个值,则将此值分配回evt
  • 如果不是,则window.event不管其内容如何分配值evt

回答by codebox

It means: if the evtparameter has a value then keep the value, if it doesn't have a value then use window.eventinstead.

这意味着:如果evt参数有值则保留该值,如果它没有值则window.event改为使用。

The ?and ':' symbols are part of the ternary if operator:

?和“:”符号是三元如果操作者的一部分:

var w = x ? y : z;

so above you assign either yor zto wdepending on whether xis considered to be a true or false value.

所以你上面指定要么y还是zw取决于是否x被认为是一个true或false值。

If the checkItfunction was called without passing in an evtargument i.e. checkIt()then inside the function the evtvariable will have the value of undefinedwhich is treated as falsewithin an if condition.

如果checkIt函数在没有传入evt参数的情况下被调用,即checkIt()在函数内部,evt变量的值undefined将被视为false在 if 条件内。