0

It seems that the XUL command and click events are somewhat different.

Although my function does get called when using the command event, the event object does not contain the button property.

My question is: how do I detect what mouse button that was pressed without using the click event?

The simplest code that can demonstrate my problem:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script language="JavaScript">
    var showMeTheButton=function(e)
    {
        alert(e.button);
    }
</script>
<button onclick="showMeTheButton(event)" label="Click me with onclick"></button>
<button oncommand="showMeTheButton(event)" label="Click me with oncommand"></button>
</window>
4

1 回答 1

2

要记住的主要事情是,oncommand任何导致按钮激活的操作都会触发它,包括在按钮获得焦点时按下空格键,使用附加到按钮的键盘快捷键,或单击按钮鼠标。事件处理程序并不真正应该关心它是如何被调用的,只关心它是如何被调用的。(我想这是一个有意识的决定,以保持一致的用户界面。)

导致按钮属性添加到 Event 对象的唯一操作是 onclickondblclickonmousedownonmouseup。您会注意到,即使您禁用了onclick按钮,事件仍然会触发,而oncommand不会触发。

要获得与 相同的功能oncommand,您需要自己处理onclickonkeydown事件,同时确保按钮仍处于启用状态。您还可以使用 XBL 创建一个自定义按钮,该按钮具有自定义事件onleftclick,如果未设置onmiddleclick则回退到oncommand- 但我没有尝试过。

于 2008-11-16T23:00:21.377 回答