当元素被 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>

更新于: 03-Mar-2020

2K+ 浏览量

开启你的 事业生涯

完成本课程并获得证书

开始
广告
© . All rights reserved.