HTML onclick 事件属性
htmlweb developmentfront end technology更新于 2024/5/20 23:40:17
当鼠标点击 HTML 文档中的 HTML 元素时,会触发 HTML onclick 属性。
语法
以下是语法 −
<tagname onclick=”script”></tagname>
示例
让我们看一个 HTML onclick 事件属性的示例 −
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
padding: 20px;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
.show {
font-size: 1.2rem;
}
</style>
</head>
<body>
<h1>HTML onclick Event Attribute Demo</h1>
<button class="btn" onclick='clickFn()'>Click me</button>
<div class="show"></div>
<script>
function clickFn() {
var msg = document.querySelector('.show');
msg.innerHTML = "";
msg.innerHTML = "Hey! you clicked me";
}
</script>
</body>
</html>
输出

点击"点击我"按钮来触发鼠标点击事件。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

