2013-07-10 81 views
0

我想弄清楚如何使用Javascript對象跟蹤多個添加和刪除計數器,並使用對象中的數據更新DOM。如何使用Javascript添加和減去DOM中的數字?

我的HTML:

<div id="first-counter-box"> 
    <div id="first-number">0</div> 
    <div id="first-add" onclick="add(first)"></div> 
    <div id="first-minus" onclick="minus(first)"></div> 
</div> 

<div id="second-counter-box"> 
    <div id="second-number">0</div> 
    <div id="second-add" onclick="add(second)"></div> 
    <div id="second-minus" onclick="minus(second)"></div> 
</div> 

我的javascript:

var dataObject = { 
    firstCounter = 0; 
    secondCounter = 0; 
} 

function add(counterId) { 
    if (counterId==first) { 
     dataObject.firstCounter++; 
     var firstNumber = document.getElementbyID("first-number"); 
     firstNumber.innerHTML(dataObject.firstCounter); 
    } 
    else (counterId==second) { 
     dataObject.secondCounter++; 
     var secondNumber = document.getElementbyID("second-number"); 
     secondNumber.innerHTML(dataObject.secondCounter); 
    } 

} 

function remove(counterId) { 
    if (counterId==first) { 
     dataObject.firstCounter--; 
     var firstNumber = document.getElementbyID("first-number"); 
     firstNumber.innerHTML(dataObject.firstCounter); 
    } 
    else (counterId==second) { 
     dataObject.secondCounter--; 
     var secondNumber = document.getElementbyID("second-number"); 
     secondNumber.innerHTML(dataObject.secondCounter); 
    } 
} 

我的目標是使用add(更新的dataObject內的數據),並刪除()函數,並更新DOM與更新的信息。

我敢肯定有一個更好的方式去做這個比我的總的JavaScript做到這一點。謝謝!

+2

馬上就對象定義完成嚴重錯誤...然後是'getElementById'的錯誤大寫......然後,「innerHTML」是一個屬性,而不是一個方法。你從來沒有定義過「第一個」和「第二個」(它們應該是字符串嗎?)總而言之,它有什麼神祕的作用? –

+0

嗨,我知道你是新來的。如果您覺得答案可以解決問題,請點擊綠色複選標記將其標記爲「已接受」。這有助於將注意力集中在仍然沒有答案的舊版SO上。 –

回答

0

大廈Kolink的評論,而是要提高生產效率,這裏是一些實實在在的幫助:

<div id="first-counter-box"> 
    <div id="first-number">0</div> 
    <div id="first-add" onclick="add('first')"></div> <-- first is a string --> 
    <div id="first-minus" onclick="minus('first')"></div> <-- first is a string --> 
</div> 

<div id="second-counter-box"> 
    <div id="second-number">0</div> 
    <div id="second-add" onclick="add('second')"></div> <-- second is a string --> 
    <div id="second-minus" onclick="minus('second')"></div> <-- second is a string --> 
</div> 

和JS:

var dataObject = { // correct object instantiation 
    firstCounter: 0, 
    secondCounter: 0 
} 

function add(counterId) { 
    if (counterId=='first') { // first is a string 
     dataObject.firstCounter++; 
     var firstNumber = document.getElementById("first-number"); // correct capitalization 
     firstNumber.innerHTML = dataObject.firstCounter; // property, not method 
    } 
    else (counterId=='second') { // second is a string 
     dataObject.secondCounter++; 
     var secondNumber = document.getElementById("second-number"); // correct capitalization 
     secondNumber.innerHTML = dataObject.secondCounter; // property, not method 
    } 

} 

function remove(counterId) { 
    if (counterId=='first') { // first is a string 
     dataObject.firstCounter--; 
     var firstNumber = document.getElementById("first-number"); // correct capitalization 
     firstNumber.innerHTML = dataObject.firstCounter; // property, not method 
    } 
    else (counterId=='second') { // second is a string 
     dataObject.secondCounter--; 
     var secondNumber = document.getElementById("second-number"); // correct capitalization 
     secondNumber.innerHTML = dataObject.secondCounter; // property, not method 
    } 
} 

這裏是一個重新寫爲更有效率:

var dataObject = { // keys match the counters 
    'first': 0, 
    'second': 0 
}; 

var divRefs = { 
    'first': document.getElementById("first-number"), 
    'second': document.getElementById("second-number") 
}; 

function add(counterId) { 
    dataObject[counterId]++; 
    divRefs[counterId].innerHTML = dataObject[counterId]; 
} 

function remove(counterId) { 
    dataObject[counterId]--; 
    divRefs[counterId].innerHTML = dataObject[counterId]; 
} 
+0

謝謝你的幫助!我試過上面的代碼,它成功地更新了dataObject,但是由於某種原因,它沒有通過innerHTML更新DOM ... – Kai

+0

getElementById仍然是大寫錯誤。 'b'需要大寫'B'。 –

+0

就是這樣。謝謝你們! – Kai