Javascript onClick函数不工作
我正试图编写一个资金管理系统,但它不能正常工作。货币打印在右上角,但当我单击按钮更改值时,它不起作用 HTML: Javascript:Javascript onClick函数不工作,javascript,Javascript,我正试图编写一个资金管理系统,但它不能正常工作。货币打印在右上角,但当我单击按钮更改值时,它不起作用 HTML: Javascript: var money = 500.27 - 0.27; var moneyElement = document.getElementById("money_count"); moneyElement.innerHTML = money; function updateMoney() { moneyElement.innerHTML = money; }
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();
}
关于为什么不更新右上角的货币价值,您有什么想法吗?您需要将事件侦听器绑定到元素:
document.getElementById('button_id').addEventListener('onclick',updateMoney,false)
onclick事件在哪里?您的代码不包含任何onclick事件。另外,您不应该真正使用Javascript浮动作为货币值。也许这可以帮助-考虑使用jQuery,它使你的生活更容易
#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;
}
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();
}