English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML 参考手册

HTML 标签大全

HTML onclick事件属性

当用户点击一个元素时,会触发 click 事件。在每次点击的整个过程中,click 事件的运行顺序在 mousedown 和 mouseup 事件之后。

HTML Event Attributes

注意:当你使用 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 ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onclick event attribute

Definition and Usage

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>.

HTML 4.01 With HTML5Differences

None.

Syntax

<element onclick="script">

Attribute Value

ValueDescription
scriptSpecifies the script to be executed when the onclick event is triggered.
HTML Event Attributes