我有一個簡單的輸入按鈕(不是提交)與onclick函數來顯示或隱藏另一個div。出於某種原因,我必須點擊按鈕兩次,一次選擇它,然後再次執行該功能。爲什麼我必須點擊這個輸入按鈕兩次來調用一個函數?
這是短版:
<input type="button" id="request_info_layer_button" onclick="showForm()" value="REQUEST INFORMATION" />
function showForm() {
var isdisplayed = document.getElementById("request_info_form_layer_wrapper").style.display;
if (isdisplayed == "none") {
document.getElementById("request_info_form_layer_wrapper").style.display = "block";
document.getElementById("request_info_layer_button").style.borderBottom = "0";
document.getElementById("request_info_layer_button").style.borderLeft = "0";
document.getElementById("request_info_layer_button").style.borderTop = "solid 3px #C4591B";
document.getElementById("request_info_layer_button").style.borderRight = "solid 4px #C4591B";
} else {
document.getElementById("request_info_form_layer_wrapper").style.display = "none";
document.getElementById("request_info_layer_button").style.borderTop = "0";
document.getElementById("request_info_layer_button").style.borderRight = "0";
document.getElementById("request_info_layer_button").style.borderLeft = "solid 3px #DFBC81";
document.getElementById("request_info_layer_button").style.borderBottom = "solid 4px #DFBC81";
}
}
完整的代碼和按鈕的行爲是在這裏:http://jsfiddle.net/yp5an1w7/3/
你必須首先設置顯示在輸入標籤的樣式屬性。只有這樣它才能在第一次點擊時工作。 – debatanu
Isdisplayed是第一次運行時的空白字符串。添加警報(isdisplayed),你會看到 – dman2306
在我的第一個評論樣式顯示:無應該被分配給div標籤。我已經發布了同樣的答案 – debatanu