[英]How can I show/hide div using Java Script on click of button
我正在嘗試編寫一個PHP Java腳本,但很難在這部分編寫中編寫。
我試圖在那個打開的形式做一個buttom
我到目前為止編寫的代碼是
function display(e){
if (e.clicked)
document.getElementById('2').style.display = 'none';
else
document.getElementById('2').style.display = 'block';
和FORM CODE是;
<input type="button" value=" Book Now " onClick="display(this)"/></input>
任何幫助指出我明顯的錯誤都會很棒,現場代碼可以在http://affordablecleaners.co.uk/quote/看到
謝謝,
亨利
嘗試類似以下的內容
var i = 0;
var display = function() {
document.getElementById('2').style.display = (i++ % 2) ? "none" : "block";
};
本質上,我們創建一個變量i
,並在每次調用函數時將其增加1。 如果在調用函數時, i
是偶數,則將其設置為display: block
。 否則,將其設置為display: none
。
該解決方案的最大缺點是全球范圍的混亂。 如果這是一個問題,您還可以執行以下操作。
var display = function() {
document.getElementById('2').style.display = (document.getElementById('2').style.display == "none") ? "block" : "none";
};
這是(未經測試的)邏輯......
function display(state, which){
if (state==1) {document.getElementById(which).style.display ='none';}
else
{document.getElementById(which).style.display = 'block';}
}
然后在你的按鈕......
打開
onclick="display('1',someDIV)"
把關掉
onclick="display('0',someDIV)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.