2013-04-21 79 views
9

編輯:由於問題已經變得相當流行,我將解決這個問題,所以它將工作的代碼示例。原來的問題仍然列出,但代碼工程用JavaScript改變DIV的可見性

我想按下按鈕後顯示一個div,但這不會工作,任何想法爲什麼是這樣的?

<form action="insert.php" method="POST" align="right" id="post_form"> 
<input type="button" value="click me" onClick="show()"> 
<div id="show_button"> fdsfds </div><br> 
</form> 

#show_button{ 
visibility:hidden; 
} 

function show(){ 
    // alert("cheked the button - worked"); 
    document.getElementById("show_button").style.visibility= 'visible' ; 
} 
+4

你應該換行ID引號:'的document.getElementById( 「show_button」)' – Antony 2013-04-21 14:51:07

+0

@Antony你是如此的權利我的朋友,要多浪費時間。 ..非常感謝上帝保佑你。 – 2013-04-21 14:53:20

回答

10

你的CSS更改爲:

#show_button{ 
display: none 
} 

而你在你的JavaScript:

function show(){ 
    //alert("cheked the button - worked"); 
    document.getElementById('show_button').style.display= 'block' ; 
} 
+0

@DavidThomas是的,我錯過了,但是,我更正了我的答案,以供進一步參考 – 2013-04-21 14:54:02

1

試試這個:

function show(){ 
    //alert("cheked the button - worked"); 
    document.getElementById("show_button").style.visibility= "visible" ; 
} 

function show(){ 
    //alert("cheked the button - worked"); 
    document.getElementById(show_button).style.display= "inline" ; 
} 
0

document.getElementById(show_button) - :語法錯誤,缺少引號「」!

+0

嗨,您正在使用哪種編輯器? – 2013-04-21 15:01:38

+0

我沒有在任何編輯器中嘗試過。無論如何,我使用Dreamweaver cs 6. – sakthi 2013-04-21 16:07:21

3

錯字錯誤改變它document.getElementById(show_button)document.getElementById("show_button")

function show(){ 
    document.getElementById("show_button").style.visibility= "visible" ; 
} 
+0

請問您可以擴展此答案嗎?也許可以解釋這會做什麼? – Kermit 2013-04-21 15:22:07