English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
当用户点击一个元素时,会触发 click 事件。在每次点击的整个过程中,click 事件的运行顺序在 mousedown 和 mouseup 事件之后。
注意:当你使用 click 事件去触发一个动作时,也要考虑向 keydown 事件添加此动作,以便允许不使用鼠标或触摸屏的用户进行同样的操作。
单击按钮时执行JavaScript:
!DOCTYPE html> <html> <head> <title>HTML onclick 事件属性的使用(基础教程网 oldtoolbag.com)</title> <script> function demo_click() { alert('哈,执行了onclick事件'); } </script> </head> <body> <button onclick="demo_click()">Try Me</button> <p>The function is triggered when the button is clicked. The function will pop up an alert box.</p> </body> </html>Test it out ‹/›
IEFirefoxOperaChromeSafari
All major browsers support the onclick event attribute
The onclick attribute is triggered when the mouse clicks on an element.
Note: The onclick attribute cannot be applied to the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.
None.
<element onclick="script">
Value | Description |
---|---|
script | Specifies the script to be executed when the onclick event is triggered. |