click与onclick
1、click()是用程序去点击按钮,而onclick()只是触发click事件去调用内部方法
2、例子
<html>
<head>
<script type="text/javascript">
function clickButton()
{
document.getElementById('button1').click()
}
function alertMsg()
{
alert("Button 1 was clicked!")
}
</script>
</head>
<body onload="clickButton()">
<form>
<input type="button" id="button1" onclick="alertMsg()" value="Button 1" />
</form>
</body>
</html>
3、可以通过HTML DOM树来分配on事件
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>点击按钮来执行 <b>displayDate()</b> 函数。</p>
<button id="myBtn">试一试</button>
<script>
document.getElementById("myBtn").onclick=function(){displayDate()}; //document.getElementById("myBtn").click=function(){displayDate()}; 同样适用
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>