【问题标题】:Why is event.ctrlKey returning undef?为什么 event.ctrlKey 返回 undef?
【发布时间】:2011-09-20 22:28:30
【问题描述】:

当在 select 元素中选择一个选项时,我正在调用一个 javascript 函数,如下所示:

<select id="select-thingy" onchange="foo(event, this); false;">
    <option value="bar">asdf</option>
...
</select>

这个函数做了这样的事情:

function foo(e, elem) {
    var thingummy = elem.options[elem.selectedIndex].value;

    alert(e.ctrlKey); // for testing only

    if (e.ctrlKey) {
        // do something
    } else {
        // do something else
    }
}

根据警报,e.ctrlKey 未定义 - 我认为这应该返回真或假?我在这里错过了什么?

谢谢大家!

【问题讨论】:

  • You're quite right。你确定你是e 变量实际上是事件吗?你试过检查window.event.ctrlKey吗?
  • 是的,我已经检查过了——我在函数内部放置了一个断点,并且 e 是一个 Event 对象。 'window.event.ctrlKey' 也返回 undefined。

标签: javascript javascript-events event-handling


【解决方案1】:

根据the standardctrlKey 属性仅适用于MouseEvents(如clickmouseover 等),但不适用于HTMLEvents。

【讨论】:

  • @FallSe7en,很高兴。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2014-04-17
  • 2010-11-19
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多