2013-05-02 144 views
0

我有這個我試圖編寫的資金管理系統,它不能正常工作。錢被打印在右上角,但是當我點擊按鈕來更改值時,它不起作用。Javascript onClick函數不能正常工作

HTML:

<div id="content"> 
<div id="money_count">$$MONEY$$</div> 
<br> 
<div id="items"> 
    <table> 
     <tr> 
      <td><a href="#">$12.19</a> 
      </td> 
      <td><a href="#">$15.00</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a href="#">$24.99</a> 
      </td> 
      <td><a href="#">$35.50</a> 
      </td> 
     </tr> 
    </table> 
</div> 
</div> 

CSS:

#content { 
font-family:Helvetica; 
width:500px; 
box-shadow:0px 0px 10px 0px #AAA; 
top:30px; 
margin:0px auto; 
margin-top:40px; 
padding:10px 20px; 
border:dashed 2px #888; 
} 
#money_count { 
    color:#0F0; 
    padding:5px; 
    text-shadow:0px 0px 1px #333; 
    margin-right:0; 
    margin-left:auto; 
    margin-top:6px; 
    width:100px; 
    text-align:center; 
} 

table, th td { 
    border:ridge 3px #AAA; 
    box-shadow:0px 0px 6px 0px #AAA; 
} 
table { 
    width:400px; 
    margin-left:50px; 
    margin-top:20px; 
    margin-bottom:50px; 
} 
td { 
    padding:4px; 
    background-color:#CCC; 
    text-align:center; 
} 
a, td { 
    color:black; 
    text-decoration:none; 
    width:auto; 
    height:100px; 
} 

的Javascript:

var money = 500.27 - 0.27; 
var moneyElement = document.getElementById("money_count"); 
moneyElement.innerHTML = money; 

function updateMoney() { 
    moneyElement.innerHTML = money; 
} 

function subtractMoney(amount) { 
    money -= amount; 
    updateMoney(); 
} 

任何想法,爲什麼這不更新右上角的貨幣價值?

+5

哪裏onclick事件? – visnu 2013-05-02 11:09:00

+0

你的代碼不包含任何onclick事件。 – 2013-05-02 11:10:34

+0

另外,你不應該真的使用Javascript浮點數作爲金錢價值。也許這可能會有所幫助 - http://josscrowcroft.github.io/accounting.js/。 – 2013-05-02 11:14:50

回答

1

您需要將事件偵聽器綁定到的元素:

document.getElementById('button_id').addEventListener('onclick', updateMoney, false)