迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

用 JavaScript 检查按钮是否被点击

作者:迹忆客 最近更新:2024/03/18 浏览次数:

在 JavaScript 中,onclick 方法和 addEventListener 是最常用的操作动作的方法。

我们将使用 onclick 作为 HTML 属性来检查 button 是否被单击。同样,我们将通过操作 DOM 继续使用 onclick 方法进行检查,稍后我们将看到使用 addEventListener 来了解按钮单击是否有效。


使用 onclick HTML 属性检查 JavaScript 中的按钮单击

在本例中,我们将采用一个 button 标签元素并添加属性 onclick

每当单击按钮时,该属性都会调用一个函数。虽然 onclick 完全是一个 JavaScript 方法,但作为一个 HTML 属性,在点击事件时触发一个指定的函数是很常见的。

代码片段:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Test</title>
  <style>
    button{
      background: gray;
      outline: none;
      border: none;
      padding: 10px;
      border-radius: 3px;
      color: white;
      cursor: pointer;
    }
  </style>
</head>
<body>  
  <button id="btn" onclick="clicked()">Click here</button>
  <script>
   const btn = document.getElementById('btn');
  function clicked(){
    btn.style.background = "purple";
    console.log("CLICKED");
  }
  </script>
</body>
</html>

输出:

在这个例子中,函数 clicked() 有一个代码体,每当单击按钮时,style background 从灰色变为紫色。


使用 onclick 作为 JavaScript 方法

JavaScript 中的 onclick 只关注一个事件来调用回调函数。该方法还强调执行在方法启动后声明的函数体。

但首先,onclick 方法需要遵循一个对象。HTML 元素的 querySelector 在这里定义了对象。

代码片段:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Test</title>
  <style>
    button{
      background: gray;
      outline: none;
      border: none;
      padding: 10px;
      border-radius: 3px;
      color: white;
      cursor: pointer;
    }
  </style>
</head>
<body>  
  <button id="btn">Click here</button>
  <script>
  const btn = document.getElementById('btn');
    btn.onclick = function(){
      btn.style.color = "yellow";
    console.log("CLICKED!");
    }
  </script>
</body>
</html>

输出:

在这里,单击事件的发生在控制台中输出为 CLICKED!并且按钮样式也可以将其字体颜色从白色更改为黄色。btn.onclick 方法开始运行相应的函数,从而可视化单击后的更改。


使用 addEventListener 方法检查按钮点击

通常,旧浏览器不支持 addEventListener 方法,但它可以处理多个事件。

在这里,我们将使用一个事件,click,该事件将处理的函数将触发按钮进行更改。此外,我们将在控制台面板中打印一些内容,以更加确定事件活动。

代码片段:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Test</title>
  <style>
    input{
      background: gray;
      outline: none;
      border: none;
      padding: 10px;
      border-radius: 3px;
      color: white;
      cursor: pointer;
    }
    p{
      background: white;
      color: gray;
    }
  </style>
</head>
<body>  
  <input type="button" id="btn" value="Click Here">
  <p id="after"></p>
  <script>
    const btn = document.getElementById('btn'); 
    function getItDone(){
      document.getElementById('after').innerHTML = "I am clicked."
      console.log("CLICKED!");
    }
    btn.addEventListener('click', getItDone);
  </script>
</body>
</html>

输出:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便