PHP前端开发

当HTML元素被点击时执行脚本?

百变鹏仔 4周前 (09-22) #HTML
文章标签 脚本

使用 onclick 属性在 HTML 中单击元素时执行脚本。

示例

您可以尝试运行以下代码来实现 onclick 属性 -
<!DOCTYPE html><html>   <body>      <button onclick = "display()">Click</button>      <p id = "test"></p>      <script>         function display() {            document.getElementById("test").innerHTML = "You clicked the button!";         }      </script>   </body></html>