In diesem Artikel wird die Methode von JS -Skripten beschrieben, um Ereignisse zu Tag -Steuerelementen dynamisch hinzuzufügen. Teilen Sie es für Ihre Referenz wie folgt weiter:
<! DocType html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transsitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <script Language = "javaScript"> Funktionset () {var obj = document.getElementById ("fy"); // obj.attachevent ('onfocus', add); // add //obj.setattribute('onfocus'Add); // Es ersetzt die ursprüngliche Ereignismethode //obj.onfocus=add;//equivalent obj.setattribute ('Onfocus', add); if(window.addEventListener){//Event codes of other browsers: Mozilla, Netscape, Firefox//The order of added events is the execution order//Note that the event with on is added with addEventListener, without adding onobj.addEventListener('focus', add, false);}else{//IE event code adds add on the original event Method obj.attachEvent ('onfocus', add);}} Funktion add () {alert ("Ereignis erfolgreich hinzugefügt");} </script> <body> <input type = "text" onfocus = "alert ('Preset Ereignis'); id = "fy" /> <input type = "button" onclick = "set ();" Value = "SSSSS"/> </body> </html>PS: Das Code -Layout scheint nicht zufriedenstellend zu sein. Hier empfehle ich Ihnen mehrere Code -Formatierungs -Tools. Ich glaube, dass es in der zukünftigen Programmierentwicklung verwendet werden kann:
C Sprachstil/HTML/CSS/JSON -Code -Formatierung und Verschönerungswerkzeuge:
http://tools.vevb.com/code/ccode_html_css_json
Online -Tools für JavaScript -Code Verschönerung und Formatierung:
http://tools.vevb.com/code/js
JavaScript Code Verschönerung/Komprimierung/Formatierung/Verschlüsselungstools:
http://tools.vevb.com/code/jscompress
Online -JSON -Code -Überprüfung, Inspektion, Verschönerung und Formatierungswerkzeuge:
http://tools.vevb.com/code/json
JSON -Code Online -Formatierung/Verschönerung/Komprimierung/Bearbeitung/Konvertieren von Tools:
http://tools.vevb.com/code/jsoncodeFormat
For more information about JavaScript, please check this site's special topics: "Summary of Ajax Operation Skills in JavaScript", "Summary of JSON Operation Skills in JavaScript", "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging Techniken "," Zusammenfassung von JavaScript -Datenstrukturen und Algorithmus -Techniken ", Zusammenfassung von JavaScript -Traversal -Algorithmen und -Techniken" und "Zusammenfassung der Verwendung von JavaScript -Mathematikoperationen"
Ich hoffe, dieser Artikel wird für JavaScript -Programme aller hilfreich sein.