在 HTML 中,当鼠标按钮按下元素时执行脚本?

htmlweb developmentfront end technology更新于 2025/8/18 5:00:17

在 HTML 中使用 onmousedown 属性,当鼠标按钮按下元素时执行脚本。

示例

您可以尝试运行以下代码来实现 onmousedown 属性 −

<!DOCTYPE html>
<html>
   <body>
      <h3 id = "myid" onmousedown = "mouseDown()" onmouseup = "mouseUp()">
         This is demo heading.
      </h3>
      <p>Click above and then release.</p>
      <script>
         function mouseDown() {
            document.getElementById("myid").style.color = "yellow";
         }
         function mouseUp() {
            document.getElementById("myid").style.color = "blue";
         }
      </script>
   </body>
</html>

相关文章


有用资源