1。通常の定義
<入力型= "ボタン" name = "button" value = "ok" onclick = "sfont =" sfont( 'red'、 'red'、 'prompt box' in the text box); if(sfont == 'red'){form1.style.fontfamily = 'bold'; form1.style.color = 'red';} "/>>これは、必要なオブジェクトでJSイベントを直接定義する最も一般的な方法です。関連する変形は、次のようにメソッドを呼び出す形式です
<script> function show(){alert( "show"); } </script> <入力型= "button" name = "show" onclick = "show()"/>2番目のタイプ
<script type = "text/javascript" for = "window" event = "onload"> alert( "welcome!"); </script> <script type = "text/javascript"
ここでは、Windowsのロードおよびアンロード時に発生する操作を定義します。
他のオブジェクトのイベントである場合、オブジェクト名への属性の値を変更するだけで、イベントは監視対象のイベントに変更されます。次のように:
<script type = "text/javascript" for = "test" event = "onclick"> alert( "button!"); </script>
3番目のタイプ:
<入力型= "button" name = "test" value = "test"/> <script> function te(){alert( "test");} test.onclick = te; </script>ここでは、登録の形式を使用して、指定されたオブジェクトの指定されたイベントにメソッドを登録します。オブジェクト名を使用して呼び出します。
完全なテストコードは次のとおりです。
< xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> untteld document </titpr> event = "onload"> alert( "welcome!"); </script> <script type = "text/javascript" for = "windo" event "event =" onunload "> alert(" anty! "); </script> <form name =" form1 "method =" post "action =" "> jsは習得が簡単です</form = <" post "" ">" "pust" value="OK" onclick="Sfont=prompt('Please enter red','Red',' prompt box ');if(Sfont=='red'){form1.style.fontFamily='bold';form1.style.color='red';}"/><input type="button" name="test" value="test"/><script>function te(){alert( "test");} test.onclick = te; </script> </form> </body> </html>JS作成イベントの上記の3つの方法(例コード)は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。