<tombol id = "btn"> klik </button> var btn = document.geteLementById ('btn');Tipe pertama:
btn.onClick = function () {alert ('hello world');}Hilangkan acara: btn.onClick = null; // kotak tidak akan muncul
Tipe kedua:
btn.addeventListener ('klik', function () {alert ('hello world')}, false); btn.addeventListener ('klik', function () {alert (this.id)}, false);Tipe ketiga:
function demo () {waspada ('hello');} <tombol id = "btn" ontClick = "demo ()"> klik </button>Berikut ini adalah panduan untuk acara klik tombol pemicu JS
Simulasi fungsi klik tombol pemicu JS
<html> <adept> <title> biasanya berfungsi </itement> </head> <script> function load () {// Dua metode berikut memiliki efek yang sama dengan document.getElementById ("target"). onclick (); document.getElementById ("target"). klik (); } function test () {alert ("test"); } </script> <body onload = "load ()"> <tombol id = "target" onclick = "test ()"> test </button> </body> <html>Komentar:
btnobj.click () adalah program penggunaan nyata untuk mengklik tombol, memicu acara tombol OnClick ()
btnobj.onClick () Cukup memanggil metode yang ditunjukkan oleh Btnobj's Onclick, cukup memanggil metode dan tidak memicu acara tersebut