简体   繁体   中英

How to unhide a hidden html <p> tag element using JavaScript?

I have the following hidden <p> element in the body tag of my HTML file (for my chrome extension).

<p hidden id="button">
    <a id="dashboard-btn" href="www.google.com" target="_blank" rel="noopener noreferrer">
      www.google.com
    </a>
</p>

I want to unhide this using JavaScript . My random try that failed to unhide it:

document.getElementById("button").style.visibility = 'visible';

[SOLVED] This (also) worked:

document.getElementById("button").style.display = "block";

You can use removeAttribute

 document.getElementById("button").removeAttribute('hidden')
 <p hidden id="button"> <a id="dashboard-btn" href="www.google.com" target="_blank" rel="noopener noreferrer"> www.google.com </a> </p>

You hide your element with attribute hidden so you need to control that attribute instead of style like:

 document.getElementById("button").hidden = false;
 <p hidden id="button"> <a id="dashboard-btn" href="www.google.com" target="_blank" rel="noopener noreferrer"> www.google.com </a> </p>

Reference:

试试这个

document.getElementById("button").removeAttribute("hidden")

您可以删除属性“隐藏”。

document.getElementById("button").removeAttribute("hidden")

I have several solutions:

A

document.getElementById("button").hidden = "false";

As hidden is not a css visibility : hidden; property.

It is an attribute.

B

As @tacoshi mentioned,

.hidden {
  opacity: 0;
  transition: opaicty 1s ease;
  /* use opacity in case you want to have a beautiful transition */
}

And just use

document.getElementById("button").classList.toggle("hidden")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM