英文:
check if click is true on eventListener
问题
const button = document.getElementById('button');
const btn = button.addEventListener("click", run);
button.disabled = true;
function run() {
if (btn.onclick == true && button.disabled == true) {
alert('按钮已被点击');
}
}
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="resources/styles/app.css">
</head>
<body>
<button id="button"></button>
</body>
<script type="module" src="./resources/js/index.js"></script>
</html>
我有一个带有 addEventListener("click", run); 的按钮。现在我想检查按钮是否被点击了或没有。我知道你可以使用 onclick 来检查,但这似乎不起作用并返回 undefined。
我有一个函数,它会在 x 分钟内禁用按钮。当按钮被禁用并且用户尝试单击它时,我想显示一个警报。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const button = document.getElementById('button');
const btn = button.addEventListener("click", run);
button.disabled = true;
function run() {
if (btn.onclick == true && button.disabled == true) {
alert('the button is clicked');
}
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="resources/styles/app.css">
</head>
<body>
<button id="button"></button>
</body>
<script type="module" src="./resources/js/index.js"></script>
</html>
<!-- end snippet -->
I have a button with an addEventListener("click", run); on it. Now I would like to check if the button is clicked yes or no. I know you can check this with onclick but this doesnt seem to work and returns undefined.
I have a function were I disable the button for x minutes. when the buttons is disabled and a user tries to click on it i want to show an alert.
答案1
得分: 1
只需删除 if
语句。每当按钮被点击时,事件监听器都会运行。
const button = document.getElementById('button');
button.addEventListener("click", run);
function run() {
alert('按钮被点击了');
}
英文:
Just get rid of the if
statement. The event listener runs whenever the button is clicked.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const button = document.getElementById('button');
button.addEventListener("click", run);
function run() {
alert('the button is clicked');
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="resources/styles/app.css">
</head>
<body>
<button id="button">Click me</button>
</body>
<script type="module" src="./resources/js/index.js"></script>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论