用 JavaScript 检查按钮是否被点击
在 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>
输出:
相关文章
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 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。