2014-04-02 31 views
0

我正在製作一個拼圖網站,您選擇一個拼圖,我想知道是否有一種方式,而不是彈出框將它打印到網站上。我不介意,我們使用的是什麼代碼,因爲我能說流利的大多數,有沒有辦法可以打印此代碼而不是使用onclick?

這是我的代碼至今:

<body> 

<script type="text/javascript"> 

function myFunction() 
{ 

     function ask() { 
    var a = (Math.round(Math.random()*1000000)) 
    alert (a) 
    return prompt("What was the number?") == eval(a); 
} 

var questions = [ask(), ask(), ask(), ask(), ask()], 
    total = questions.length, 
    correct = questions.filter(Boolean).length; 

alert("You got "+correct+"/"+total+" correct"); 

} 

</script> 

<button onClick="myFunction()">Remember the number</button> 


</body> 

<body> 

<script type="text/javascript"> 
function myFunction2(){ 

     function ask() { 
    var a = Math.floor(Math.random() * 10) + 1; 
    var b = Math.floor(Math.random() * 10) + 1; 
    var op = ["*", "+", "/", "-"][Math.floor(Math.random()*4)]; 
    return prompt("How much is " + a + " " + op + " " + b + "?") == eval(a + op + b); 
} 

var questions = [ask(), ask(), ask(), ask(), ask()], 
    total = questions.length, 
    correct = questions.filter(Boolean).length; 

alert("You got "+correct+"/"+total+" correct"); 
} 
</script> 

<button onClick="myFunction2()">Quick math</button> 

</body> 

</html> 
</html> 

所以我在想,如果有辦法使它顯示爲文本並在頁面上有一個文本框來輸入,該文本仍然有效。而且設計可以改變,所以我可以製作更大的文本框或更大的字體,所以它不僅僅是一個不可編輯的onclick。

所有幫助非常感謝。 謝謝。

+3

看一看jQuery用戶界面對話框https://開頭jQueryUI的。 com/dialog/ – avrono

+0

看看jquery'.append()'和'.remove()' – blgt

+0

謝謝你們倆 – WTFreezer

回答

2

以及我做了什麼,我認爲u想「記住數」按鈕,點擊.... 抱歉沒時間做另一個....堅持我的工作

下面是代碼

HTML

<body> 


<button id="rmbrBtn">Remember the number</button> 


</body> 

<body> 


<button id="quivkBtn">Quick math</button> 
    <div id="question_area"> 

    </div> 
</body> 

</html> 
</html> 

JS & jQuery代碼

$("#rmbrBtn").click(function() 
{ 
    // Answers collection 
    var questions = []; 

     // Check the correctness of the answer 
     function checkAnswer(){ 
       total = questions.length, 
       correct = questions.filter(Boolean).length; 
      if(total < 5) 
      { 
       ask(); 
      }else{ 
       var answer = '<div>You got '+correct+'/'+total+' correct <input type="button" value="Ok" id="ansOk"/></div>'; 
       $("#question_area").append(answer); 
       $("#ansOk").click(function(){ 
        $(this).parent().empty(); 
        $(this).parent().remove(); 
       }); 
      } 
     } 

     // Get the question 
     function ask() { 
      var a = (Math.round(Math.random()*1000000)); 
      // View the generated number 
      var viewNumber = '<div>'+a+'<input type="button" id="ok" value="OK"/>'+'</div>'; 

      // Prompt user with a text box 
      var promptVal = '<div>Enter your value: <input type="text" id="ans" /> <input type="button" id="prmtOk" value="Ok"/></div>'; 

      // Append 
      $("#question_area").append(viewNumber); 

      $("#ok").click(function(){ 
       $(this).parent().empty(); 
       $(this).parent().remove(); 

       $("#question_area").append(promptVal); 

       $("#prmtOk").click(function(){ 
        var prmt = $("#ans").val(); 
        var addVal = prmt == a; 
        questions.push(addVal); 
        checkAnswer(); 
        $(this).parent().empty(); 
        $(this).parent().remove(); 
       }); 
      }); 
     } 

    // Run the function. 
     checkAnswer(); 
}); 

Online解決方案: JSFiddle

嘗試做另外一個與此相同....

抱歉沒有時間發表評論也... :D

我覺得你可以算出來...... 希望這是ü想要什麼.... 請投我... 感謝 享受

+0

完美!這很有效 – WTFreezer

+0

Thanx ....問我是否需要更多幫助... –

0

這也可以不用JQuery和JavaScript就可以完成。
http://jsfiddle.net/8nWKD/4/

<div id="header" class="header">This is the header</div> 
<div class="button" onClick="test_click()"> Click me!</div> 

和JavaScript

function test_click() 
{ 
    document.getElementById("header").innerHTML = "CORRECT!"; 
} 
相關問題