我试图让按钮单击导致变量增加1,然后更新显示变量的页面计数器。出于某种原因,在页面加载时,该按钮显然会点击一次,并且任何实际的点击都不会执行任何操作。按钮在加载时自动点击一次,然后无法点击
我试过$("#click").click(click(1));
并将onClick="click(1)"
放在按钮的实际HTML上,但两者似乎都输出了相同的结果。
下面是相关的HTML:
<div class="column_middle">
<div style="font-size:115%;display:inline;">Money: <span id="money">0</span></div>
<button id="click" class="mainbutton" style="margin:10px;margin-right:5px;" title="">Click</button>
及相关的javascript:
// Activated on DOM load
function onLoad(jQuery) {
// Set up button functions
$("#click").click(click(1));
}
function click(amount) { // Player clicking
money += amount; // Add the amount of clicks to the total money
html_money.text(money); // Update page money value
console.log("Clicked "+amount+" times.");
}
// Variable declaration
var money = 0; // Total money
var stock = 0; // Total stock
var html_money = $("#money"); // Money variable on the actual page
var html_stock = $("#stock"); // Stock variable on the actual page
检查下面 – Shehabix