Javascript 单击按钮显示内容时如何隐藏按钮?

Javascript 单击按钮显示内容时如何隐藏按钮?,javascript,html,css,Javascript,Html,Css,我想在单击按钮时隐藏它。但我希望在单击按钮时显示我的内容 #sectiontohide{ display: none;} function toggle_div_fun(id) { var divelement = document.getElementById(id); if(divelement.style.display == 'none') divelement.style.display = 'block'; else divelemen

我想在单击按钮时隐藏它。但我希望在单击按钮时显示我的内容

#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">`
这是我想在单击按钮时显示的内容,按钮应消失

请尝试以下操作:

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>
可能重复的