2015-11-05 92 views
0

我想隱藏按鈕,當我點擊它。但是當我點擊按鈕時,我希望顯示我的內容。如何隱藏按鈕,當我點擊按鈕來顯示內容?

#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

+0

[隱藏按鈕的可能的複製點擊](http://stackoverflow.com/questions/32373038/hiding-a-button-on-click) – BSMP

回答

0

試試這個:

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> 
相關問題