2013-11-05 96 views
-3

我有有如何在javascript中進行簡單的計算?

<div id="totalscore"></div> 

一個div我有一個變種名爲totalscore從DIV

var totalscore = document.getElementById("totalscore"); 
    var score = 1; 
    var score_text = "Score:" + score; 

獲得信息我如何計算總得分,並將其顯示到一個評分系統div?

+2

爲什麼不'totalscore.innerHTML = score_text' –

+0

VAR得分= parseInt函數( 「得分加」)+ totalscore.innerHTML; –

回答

0

嘗試這樣的事情

var totalscore = document.getElementById("totalscore"); 
    var score = 1; 
    var score_text = "Score:" + score; 
    totalscore.innerHTML = score_text; 

編輯的代碼

var totalscore = document.getElementById("totalscore"); 
    // get only number from string 
    var existing_score = totalscore.innerHTML.replace(/^\D+/g, ''); 

    // parsing string to number 
    var score = 1 + parseInt(existing_score); 
    var score_text = "Score:" + score; 

    //replacing with new score 
    totalscore.innerHTML = score_text; 
+0

感謝您的幫助。但是,當我點擊一個按鈕時,如何將1添加到當前分數 – Untitled

+0

將此代碼放入一個函數中並使用onclick事件製作一個按鈕。 –