2011-09-20 43 views
0

当在选择元件选择像这样的选择,我调用javascript函数:为什么event.ctrlKey返回undef?

<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是未定义 - 我认为这应该返回true或false?我在这里错过了什么?

谢谢大家!

+0

[你很对](https://developer.mozilla.org/en/DOM/event.ctrlKey)。你确定你是'变量'实际上是事件吗?你有没有试过检查'window.event.ctrlKey'? – Alex

+0

是的,我检查过 - 我把一个断点放在函数的内部,e是一个Event对象。 'window.event.ctrlKey'也返回undefined。 – Justin

回答

6

作为每the standard,属性ctrlKey只在MouseEvent的可用(如clickmouseover等),但不HTMLEvent秒。

+0

啊,这很有道理。谢谢!我想我将不得不重新考虑我的方法... – Justin

+0

@ FallSe7en,很高兴。祝你好运。 – davin