按钮在单击后消失 Js/html

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

Buttons disappearing after clicking on them Js/html

问题

你好,我尝试编写一个计数器代码,在其中有两个按钮(增加和减少),我创建了函数,它确实起作用,但按钮消失了。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Buttons practice</title>
  8. <script src="/JavaScript/buttons_practice.js" defer></script>
  9. </head>
  10. <body>
  11. <button id="todo-button" onclick="DoneChange()">Changing Text</button>
  12. <div id="counter">0
  13. <button id="up">Up</button>
  14. <button id="down">Down</button>
  15. </div>
  16. <div id="counter">0</div>
  17. </body>
  18. </html>
  19. const Counter = document.getElementById('counter');
  20. const Up = document.getElementById('up');
  21. const Down = document.getElementById('down');
  22. let count = 0;
  23. Up.addEventListener('click', function Increase(){
  24. count = count + 1;
  25. UpdateCounter();
  26. });
  27. Down.addEventListener('click', function Decrease() {
  28. count = count -1;
  29. UpdateCounter();
  30. });
  31. function UpdateCounter() {
  32. Counter.innerText= count;
  33. }

我在阅读一些类似的问题,但我无法弄清楚。有些答案指向 InnerText 有问题,但是我可以看到这段代码是有效的。我基本上使用事件侦听器,以便我可以在 Html 中拥有独占的 Html,这使得代码更具模块性,更易于阅读。

  1. <div id="counter">0</div>
  2. <button onclick="countUp()">Up</button>
  3. <button onclick="countDown()">Down</button>
  4. <script>
  5. let count = 0;
  6. function countUp() {
  7. count = count + 1;
  8. updateCount();
  9. }
  10. function countDown() {
  11. count = count - 1;
  12. updateCount();
  13. }
  14. function updateCount() {
  15. let counter = document.getElementById('counter');
  16. counter.innerText = count;
  17. }
  18. </script>
英文:

`Hello there, I'm trying to write a counter code in which I have two buttons (Increase and Decrease), I created the functions, and it works but the buttons disappear.

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Buttons practice&lt;/title&gt;
  8. &lt;script src=&quot;/JavaScript/buttons_practice.js&quot; defer&gt;&lt;/script&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;button id=&quot;todo-button&quot; onclick=&quot;DoneChange()&quot;&gt;Changing Text&lt;/button&gt;
  12. &lt;div id=&quot;counter&quot;&gt;0
  13. &lt;button id=&quot;up&quot;&gt;Up&lt;/button&gt;
  14. &lt;button id=&quot;down&quot;&gt;Down&lt;/button&gt;
  15. &lt;/div&gt;
  16. &lt;div id=&quot;counter&quot;&gt;0&lt;/div&gt;
  17. &lt;/body&gt;
  18. &lt;/html&gt;
  19. const Counter = document.getElementById(&#39;counter&#39;);
  20. const Up = document.getElementById(&#39;up&#39;);
  21. const Down = document.getElementById(&#39;down&#39;);
  22. let count = 0;
  23. Up.addEventListener(&#39;click&#39;, function Increase(){
  24. count = count + 1;
  25. UpdateCounter();
  26. });
  27. Down.addEventListener(&#39;click&#39;, function Decrease() {
  28. count = count -1;
  29. UpdateCounter();
  30. });
  31. function UpdateCounter() {
  32. Counter.innerText= count;
  33. }

I was reading some similar questions, but I couldn't figure it out. Some answers aimed to the InnerText to be the Issue, however I can see this code works, I'm basically using the event listener so I can have Html exclusive to Html, it's modular and better at reading the code I think.

  1. &lt;div id=&quot;counter&quot;&gt;0&lt;/div&gt;
  2. &lt;button onclick=&quot;countUp()&quot;&gt;Up&lt;/button&gt;
  3. &lt;button onclick=&quot;countDown()&quot;&gt;Down&lt;/button&gt;
  4. &lt;script&gt;
  5. let count = 0;
  6. function countUp() {
  7. count = count + 1;
  8. updateCount();
  9. }
  10. function countDown() {
  11. count = count - 1;
  12. updateCount();
  13. }
  14. function updateCount() {
  15. let counter = document.getElementById(&#39;counter&#39;);
  16. counter.innerText = count;
  17. }
  18. &lt;/script&gt;

答案1

得分: 2

按钮位于计数器内部。因此,当您使用 innerText 重写计数器的内容时,会替换按钮。

不要将按钮放在计数器内部。

英文:

The buttons are inside the counter. Consequently, when you rewrite the contents of the counter (with innerText) you replace the buttons.

Don’t put the buttons inside the counter.

答案2

得分: 1

在这个块中,您有两个ID为“counter”的元素:

  1. <div id="counter">0
  2. <button id="up">Up</button>
  3. <button id="down">Down</button>
  4. </div>
  5. <div id="counter">0</div>

当您执行

  1. Counter.innerText= count;

您会覆盖第一个包含按钮的“counter” div 中的所有内容,因此它们会消失。

英文:

You have two elements with id counter in this block:

  1. &lt;div id=&quot;counter&quot;&gt;0
  2. &lt;button id=&quot;up&quot;&gt;Up&lt;/button&gt;
  3. &lt;button id=&quot;down&quot;&gt;Down&lt;/button&gt;
  4. &lt;/div&gt;
  5. &lt;div id=&quot;counter&quot;&gt;0&lt;/div&gt;

When you do

  1. Counter.innerText= count;

you override everything that you have within the first counter div that contains buttons, and as a result, they disappear.

huangapple
  • 本文由 发表于 2023年2月19日 08:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497288.html
匿名

发表评论

匿名网友

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

确定