HTML ontoggle 事件属性
htmlweb developmentfront end technology更新于 2025/12/21 1:16:17
当用户打开或关闭 HTML 文档中的 <details> HTML 元素时,会触发 HTML ontoggle 事件属性。
语法
以下是语法 −
<details ontoggle=”script”>Content</details>
让我们看一个 HTML ontoggle 事件属性的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
text-align: center;
padding: 20px;
}
</style>
</head>
<body>
<h1>HTML ontoggle Event Attribute Demo</h1>
<details ontoggle="toggleFn()">
<summary>Hi! I'm summary element</summary>
<p>This is a paragraph with some dummy content.</p>
</details>
<script>
function toggleFn() {
document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
document.body.style.color = "#fff";
}
</script>
</body>
</html>
输出

现在尝试打开详细信息 HTML 元素来观察 ontoggle 事件属性如何工作。

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

