如何使用JavaScript检查是否单击了Button

问题描述:

我只是好奇是否有一种简单的方法可以按照这些方式做点什么:

I'm just curious if there is a simple way to do something along these lines:

javascript:

javascript:

if(document.getElementById('button').clicked == true)
{
   alert("button was clicked");
}

html:

<input id="button" type="submit" name="button" value="enter"/>


您可以为此添加点击事件处理程序:

You can add a click event handler for this:

document.getElementById('button').onclick = function() {
   alert("button was clicked");
}​;​

如果你想跟踪它,它会在点击时发出警告以后,只需在该函数中将变量设置为true而不是警告,或者变量++ 如果您想计算点击次数,无论您的最终用途是什么。 您可以在此处查看示例

This will alert when it's clicked, if you want to track it for later, just set a variable to true in that function instead of alerting, or variable++ if you want to count the number of clicks, whatever your ultimate use is. You can see an example here.