2013-07-10 88 views
1

非常簡單,我想在我的頁面上多次使用下面的代碼來處理多個'boxes',那麼當它被調用時如何傳遞參數,即調用隱藏(box1ID)會隱藏box1ID等等.....傳遞一個函數的參數

function conceal() {  
     if(document.getElementById('box1ID').style.display=='block') { 
      document.getElementById('box1ID').style.display='none'; 
     } 
     return false; 
    } 

function show() { 
    if(document.getElementById('box1ID').style.display=='none') { 
     document.getElementById('box1ID').style.display='block'; 
    } 
    return false; 
} 
+0

如果您問如何將參數傳遞給函數,那是非常基本的。這是我期望在閱讀介紹性教程/書籍時學到的。我誤解了你的問題嗎? –

回答

1

我不知道你需要什麼。是這樣的嗎?

function conceal(boxId) {  
     if(document.getElementById(boxId).style.display=='block') { 
      document.getElementById(boxId).style.display='none'; 
     } 
     return false; 
    } 

function show(boxId) { 
    if(document.getElementById(boxId).style.display=='none') { 
     document.getElementById(boxId).style.display='block'; 
    } 
    return false; 
} 




show('box1ID'); 
conceal('box1ID'); 
2

它非常簡單,只寫它,包括它...

function conceal(element) {  
     if(document.getElementById(element).style.display=='block') { 
      document.getElementById(element).style.display='none'; 
     } 
     return false; 
    } 

    function show(element) { 
     if(document.getElementById(element).style.display=='none') { 
      document.getElementById(element).style.display='block'; 
     } 
    return false; 
    } 

Call it like so: 
conceal('box1ID'); 
1

你的意思是這樣嗎?

function conceal(boxID) {  
    if(document.getElementById(boxID).style.display=='block') { 
     document.getElementById(box1ID).style.display='none'; 
    } 
    return false; 
} 

function show(boxID) { 
    if(document.getElementById(boxID).style.display=='none') { 
     document.getElementById(boxID).style.display='block'; 
    } 
    return false; 
} 
0
<input type="Button" onclick="conceal(this.id)"/> 

的Javascript:

function conceal(buttonId) {  
    if(document.getElementById('+buttonId+').style.display=='block') { 
     document.getElementById('+buttonId+').style.display='none'; 
    } 
    return false; 
} 
1

這裏我節省一些代碼

function showhide(id,show) {  
    document.getElementById(id).style.display=show?'block':'none'; 
    return false; 
} 

使用內聯(我假設你使用內聯由於返回false)

<a href="#" onclick="return showhide('box1ID',true)">Show</a> 
<a href="#" onclick="return showhide('box1ID',false)">Hide</a> 

要切換使用

function toggle(id) {  
    document.getElementById(id).style.display=document.getElementById(id).style.display=="block"?"none":"block"; 
    return false; 
} 

內聯使用(我假設你使用內聯,因爲重新轉爲假)

<a href="#" onclick="return toggle('box1ID')">Toggle</a> 
相關問題