2013-05-27 60 views
-1

我有多個按鈕與相關文本到每個按鈕,當用戶單擊按鈕時,文本應根據按鈕更改和文本應顯示在DIV中。當JavaScript onclick顯示文本

我正在使用ifif如果在每個按鈕的文本之間進行選擇,現在我無法通過函數將文本傳遞給div onclick()。

<html> 
    <head> 
     function display (selected) { 
     if (decision == firstbox) { 
      display = "the text related to first box should be displayed"; 
     } else if (decision == secondbox) { 
      display = "Text related to 2nd box."; 
     } else { 
      display ="blank"; 
     } 
    </head> 
    <body> 
     <input type="button" id="firstbox" value= "firstbox" onclick="display(firstbox)" /><br>  
     <input type="button" id="secondbox" value= "secondbox" onclick="display(firstbox)" /><br> 
    </body> 
</html> 
+0

您可以發佈您的代碼? –

+3

嘗試在這裏發佈前輸入您的問題谷歌 –

+0

功能顯示(選擇){ if(decision == firstbox) { display =「與第一個框相關的文本應顯示」; } else if(decision == secondbox) { display =「與2nd box相關的文本。 } ... ...。 ...。

user2305807

回答

1

從您的代碼純JavaScript:

function display (selected) 
    { 
    if (selected == 'firstbox') 
    { 
    texttoshow = "the text related to first box should be displayed"; 
    } 
    else if (selected == 'secondbox') 
    { 
    texttoshow = "Text related to 2nd box."; 
    } 
    document.getElementById("thetext").innerHTML = texttoshow; 
    } 

和HTML:

<body> 
    <div id = "thetext"></div> 
    <button onclick = "display(firstbox)">Firstbox</button> 
    <button onclick = "display(secondbox)">Secondbox</button> 
</body> 

對於什麼是值得的,在jQuery的(一個JavaScript框架):

$("#buttonclicked"). 
    click(function(){ 
    $("#yourdiv"). 
     html("Your text"); 
    }); 
+0

謝謝你解決了我的問題 – user2305807

+1

然後你可以'接受'我的回答(; –

0

在這裏,我要爲你例子之一。其中一個使用div另一個不使用鏈接,另一個使用需要點擊的按鈕。

<!DOCTYPE html> 
<html> 
<body> 

<h2>Displaying text when clicked</h2> 

<button type="button" 
onclick="document.getElementById('demo').innerHTML = 'These are the steps to get your PIN number: Bla bla bla'"> 
PIN button:</button> 

<p id="demo"></p> 


</br></br></br> 

<a onclick="showText('text1')" href="javascript:void(0);">PIN link:</a> 

<script language="JavaScript"> 
    function showText(id) 
    { 
     document.getElementById(id).style.display = "block"; 
    } 
</script> 

<div id="text1" style="display:none;">These are the steps to get your PIN number: Bla bla bla</div> 

</body> 
</html>