当单击按钮时切换元素的可见性。

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

Toggle the visibility of an element when a button is clicked

问题

我正在尝试执行一个练习,通过访问元素的ID来切换元素的可见性。当我点击它时,我可以使其消失。然而,我不明白我的代码有什么问题,以至于它在点击后不会重新出现。

let button = document.getElementById("button");

document.getElementById("button").onclick = function() {
  if (button.style.display === "none") {
    button.style.display = "block";
  } else {
    button.style.display = "none";
  }
};
#button {
  display: block;
}
<p id="p1Text">Hello world</p>
<button id="button">Buttonni</button>

希望这可以帮助你解决问题。

英文:

I am trying to perform an exercise where I toggle the visibility of an element by accessing its ID. I can get it to disappear when I click it. However, I don't understand what is wrong with my code so that it doesn't reappear when clicked.

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

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

let button = document.getElementById(&quot;button&quot;);

document.getElementById(&quot;button&quot;).onclick = function() {
  if (button.style.display === &quot;none&quot;) {
    button.style.display = &quot;block&quot;;
  } else {
    button.style.display = &quot;none&quot;;
  }
};

<!-- language: lang-css -->

#button {
  display: block;
}

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

&lt;p id=&quot;p1Text&quot;&gt;Hello world&lt;/p&gt;
&lt;button id=&quot;button&quot;&gt;Buttonni&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

我建议使用element.classList.toggle来实现这种情况。

https://www.w3schools.com/howto/howto_js_toggle_class.asp

const hello = document.querySelector('.hello');

document.getElementById("button").onclick = function() {
  hello.classList.toggle('hidden');
};
.hidden {
  display: none;
}
<p id="p1Text" class="hello">Hello world</p>
<button id="button">Buttonni</button>
英文:

I would refer to use element.classList.toggle to achieve this senario.

https://www.w3schools.com/howto/howto_js_toggle_class.asp

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

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

const hello = document.querySelector(&#39;.hello&#39;);

document.getElementById(&quot;button&quot;).onclick = function() {
  hello.classList.toggle(&#39;hidden&#39;);
};

<!-- language: lang-css -->

.hidden {
  display: none;
}

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

&lt;p id=&quot;p1Text&quot; class=&quot;hello&quot;&gt;Hello world&lt;/p&gt;
&lt;button id=&quot;button&quot;&gt;Buttonni&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

你点击按钮后让按钮消失了。这是你想要让它消失的意思吗?

还是你想在点击按钮后让以下段落消失?

<p id="p1Text">Hello world</p>

如果你想让这段文字消失,你需要将 getElementById 更改为正确的ID。

目前你这样做:

let button = document.getElementById("button"); 

这获取到了按钮的ID,相反,你应该获取段落的元素:

let button = document.getElementById("p1Text"); 

这应该可以解决问题。

英文:

You are making the button disappear once it is clicked. Is that what you mean to make disappear?

Or are you trying to make the following paragraph disappear upon button click?

&lt;p id=&quot;p1Text&quot;&gt;Hello world&lt;/p&gt;

If that is what you want to make disappear, you have to change the getElementById to the correct ID.

Currently, you are doing:

let button = document.getElementById(&quot;button&quot;); 

which is getting the ID of the button, instead, get the element of the paragraph

let button = document.getElementById(&quot;p1Text&quot;); 

That should fix it

huangapple
  • 本文由 发表于 2023年5月29日 04:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353533.html
匿名

发表评论

匿名网友

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

确定