检查事件监听器中的 click 是否为 true。

huangapple go评论89阅读模式
英文:

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(&#39;button&#39;);
const btn = button.addEventListener(&quot;click&quot;, run);

button.disabled = true;

function run() {
  if (btn.onclick == true &amp;&amp; button.disabled == true) {
    alert(&#39;the button is clicked&#39;);
  }
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;nl&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/styles/app.css&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;button id=&quot;button&quot;&gt;&lt;/button&gt;
&lt;/body&gt;

&lt;script type=&quot;module&quot; src=&quot;./resources/js/index.js&quot;&gt;&lt;/script&gt;

&lt;/html&gt;

<!-- 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(&#39;button&#39;);
button.addEventListener(&quot;click&quot;, run);

function run() {
  alert(&#39;the button is clicked&#39;);
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;nl&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/styles/app.css&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;button id=&quot;button&quot;&gt;Click me&lt;/button&gt;
&lt;/body&gt;

&lt;script type=&quot;module&quot; src=&quot;./resources/js/index.js&quot;&gt;&lt;/script&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 01:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定