如何隐藏按钮,当我点击按钮来显示内容?
问题描述:
我想隐藏按钮,当我点击它。但是当我点击按钮时,我希望显示我的内容。如何隐藏按钮,当我点击按钮来显示内容?
#sectiontohide{
display: none;}
function toggle_div_fun(id) {
var divelement = document.getElementById(id);
if(divelement.style.display == 'none')
divelement.style.display = 'block';
else
divelement.style.display = 'none';
}
<button onclick="toggle_div_fun('sectiontohide');">Display Content</button>
<div id="sectiontohide">`
this is the content i'd like to show when i click the button and button should disappear
答
试试这个:
function toggle_div_fun(id, btn) {
var divelement = document.getElementById(id);
btn.style.display = "none";
if(divelement.style.display == 'none')
divelement.style.display = 'block';
else
divelement.style.display = 'none';
}
<button onclick="toggle_div_fun('sectiontohide', this);">Display Content</button>
<div id="sectiontohide" style="display:none">Content</div>
如果你不需要重用的代码,也许这是简单的:
<button onclick="document.getElementById('sectiontohide').style.display='block'; this.style.display='none'">Display Content</button>
<div id="sectiontohide" style="display:none">Content</div>
[隐藏按钮的可能的复制点击](http://stackoverflow.com/questions/32373038/hiding-a-button-on-click) – BSMP