command
元素响应不同来源的、对于同一操作的操作请求。比如,剪贴板的粘贴操作,可以来自 “编辑”菜单,可以来自鼠标右键的上下文菜单,也可以来自键盘快捷键。你可以通过 command 的 oncommand
属性调用代码。对于用户来说,从什么地方触发了这个操作并不重要。另外,关闭 command 将自动关闭菜单中的项目和对键盘快捷键的响应。Commands are identified by their
. If you include the script id
chrome://global/content/globalOverlay.js
in your window, you can use the function goDoCommand
function to invoke the command. Using this function has the advantage that the command will be sent to the part of the UI which will respond to it. Typically, this will be the currently focused element.
Like a broadcaster
, commands forward attributes to other elements.
More information is available in the XUL tutorial. See also: command
attribute, commandset
element
Examples
The following code will send a paste command (cmd_paste
) to the currently focused element:
// First include chrome://global/content/globalOverlay.js goDoCommand("cmd_paste");
Example with two buttons
<command id="cmd_openhelp" oncommand="alert('Help');"/> <button label="Help" command="cmd_openhelp"/> <button label="More Help" command="cmd_openhelp"/>
Attributes
-
disabled
- Type: boolean
- Indicates whether the element is disabled or not. If this element is set to
true
the element is disabled. Disabled elements are usually drawn with grayed-out text. If the element is disabled, it does not respond to user actions, it cannot be focused, and thecommand
event will not fire.
<!-- Checkbox enables/disables the button --> <checkbox label="Enable button" onclick="document.getElementById('buttRemove').disabled = this.checked"/> <button id="buttRemove" label="Remove All" disabled="true"/>
label
- Type: string
- The label that will appear on the element. If this is left out, no text appears.
label 在元素上显示。如果左侧出界,则不显示任何文字。
oncommand
- Type: script code
- This event handler is called when the command is activated. This occurs when a user selects a menu item or presses a keyboard shortcut attached to the command.
Properties
Inherited Properties |