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

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

check if click is true on eventListener

问题

  1. const button = document.getElementById('button');
  2. const btn = button.addEventListener("click", run);
  3. button.disabled = true;
  4. function run() {
  5. if (btn.onclick == true && button.disabled == true) {
  6. alert('按钮已被点击');
  7. }
  8. }
  1. <!DOCTYPE html>
  2. <html lang="nl">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="resources/styles/app.css">
  7. </head>
  8. <body>
  9. <button id="button"></button>
  10. </body>
  11. <script type="module" src="./resources/js/index.js"></script>
  12. </html>

我有一个带有 addEventListener("click", run); 的按钮。现在我想检查按钮是否被点击了或没有。我知道你可以使用 onclick 来检查,但这似乎不起作用并返回 undefined。

我有一个函数,它会在 x 分钟内禁用按钮。当按钮被禁用并且用户尝试单击它时,我想显示一个警报。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const button = document.getElementById(&#39;button&#39;);
  2. const btn = button.addEventListener(&quot;click&quot;, run);
  3. button.disabled = true;
  4. function run() {
  5. if (btn.onclick == true &amp;&amp; button.disabled == true) {
  6. alert(&#39;the button is clicked&#39;);
  7. }
  8. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;nl&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/styles/app.css&quot;&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;button id=&quot;button&quot;&gt;&lt;/button&gt;
  10. &lt;/body&gt;
  11. &lt;script type=&quot;module&quot; src=&quot;./resources/js/index.js&quot;&gt;&lt;/script&gt;
  12. &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 语句。每当按钮被点击时,事件监听器都会运行。

  1. const button = document.getElementById('button');
  2. button.addEventListener("click", run);
  3. function run() {
  4. alert('按钮被点击了');
  5. }
英文:

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 -->

  1. const button = document.getElementById(&#39;button&#39;);
  2. button.addEventListener(&quot;click&quot;, run);
  3. function run() {
  4. alert(&#39;the button is clicked&#39;);
  5. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;nl&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link rel=&quot;stylesheet&quot; href=&quot;resources/styles/app.css&quot;&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;button id=&quot;button&quot;&gt;Click me&lt;/button&gt;
  10. &lt;/body&gt;
  11. &lt;script type=&quot;module&quot; src=&quot;./resources/js/index.js&quot;&gt;&lt;/script&gt;
  12. &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:

确定