本文實(shí)例講述了JS簡(jiǎn)單模擬觸發(fā)按鈕點(diǎn)擊功能的方法。分享給大家供大家參考,具體如下:
<html> <head> <title>usually function</title> </head> <script>function load(){ //下面兩種方法效果是一樣的 document.getElementById("target").onclick(); document.getElementById("target").click();}function test(){ alert("test");}</script> <body onload="load()"> <button id="target" onclick="test()">test</button> </body><html>
備注:
btnObj.click()是真正地用程序去點(diǎn)擊按鈕,觸發(fā)了按鈕的onclick()事件
btnObj.onclick()只是簡(jiǎn)單地調(diào)用了btnObj的onclick所指向的方法,只是調(diào)用方法而已,并未觸發(fā)事件
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。