• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Dynamisch Event einem Element zuweisen.

Status
Für weitere Antworten geschlossen.

Puccini

Neues Mitglied
Hi

ich will einem Element dynamisch mehrere Funktionen bei onclick zuweisen, doch leider scheitere ich an einer fehlermeldung...

Hier die Funktion zum Adden:
Code:
[LIST=1]
[*][FONT=monospace][COLOR=#666666][I]/*[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#666666][I]    fügt einem bestimmten Objekt ein weiters Event hinzu[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#666666][I]    @param String ObjektID Die ID des HTML-Objektes[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#666666][I]    @param String Event Das Event welches erweitert werden soll (z..B: onclick)[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#666666][I]    @param String FunctionName der name der Funktion welche angehangen wird (mit () am Ende)[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#666666][I]*/[/I][/COLOR][/FONT]
[*][FONT=monospace][COLOR=#000000][B]function[/B][/COLOR] add_event[COLOR=#009900]([/COLOR]ObjektID[COLOR=#339933],[/COLOR] Event[COLOR=#339933],[/COLOR] FunctionName[COLOR=#009900])[/COLOR][/FONT]
[*][FONT=monospace][COLOR=#009900]{[/COLOR][/FONT]
[*][FONT=monospace]    [COLOR=#000000][B]var[/B][/COLOR] tmpObjekt [COLOR=#339933]=[/COLOR] document[COLOR=#339933].[/COLOR]getElementById[COLOR=#009900]([/COLOR]ObjektID[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[*][FONT=monospace]    [COLOR=#b1b100]if[/COLOR][COLOR=#009900]([/COLOR]tmpObjekt[COLOR=#009900])[/COLOR][/FONT]
[*][FONT=monospace]    [COLOR=#009900]{[/COLOR][/FONT]
[*][FONT=monospace]        [COLOR=#000000][B]var[/B][/COLOR] oldEvent [COLOR=#339933]=[/COLOR] String[COLOR=#009900]([/COLOR]tmpObjekt[COLOR=#339933].[/COLOR]attributes[COLOR=#009900][[/COLOR][COLOR=#0000ff]Event[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[*][FONT=monospace]        alert[COLOR=#009900]([/COLOR]oldEvent[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[*][FONT=monospace]        [COLOR=#b1b100]if[/COLOR][COLOR=#009900]([/COLOR]oldEvent [COLOR=#339933]&&[/COLOR] oldEvent[COLOR=#339933].[/COLOR]length [COLOR=#339933]>[/COLOR][COLOR=#cc66cc]0[/COLOR][COLOR=#009900])[/COLOR][/FONT]
[*][FONT=monospace]            tmpObjekt[COLOR=#339933].[/COLOR]attributes[COLOR=#339933].[/COLOR]add[COLOR=#009900]([/COLOR]Event[COLOR=#339933],[/COLOR]oldEvent[COLOR=#339933]+[/COLOR][COLOR=#0000ff]";"[/COLOR][COLOR=#339933]+[/COLOR]FunctionName[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[*][FONT=monospace]        [COLOR=#b1b100]else[/COLOR][/FONT]
[*][FONT=monospace]            tmpObjekt[COLOR=#339933].[/COLOR]attributes[COLOR=#339933].[/COLOR]add[COLOR=#009900]([/COLOR]Event[COLOR=#339933],[/COLOR]FunctionName[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR][/FONT]
[*][FONT=monospace]    [COLOR=#009900]}[/COLOR][/FONT]
[*][FONT=monospace][COLOR=#009900]}[/COLOR][/FONT]
[/LIST]
Die fehlermeldung:
Code:
[FONT=monospace]Fehler[COLOR=#339933]:[/COLOR] tmpObjekt[COLOR=#339933].[/COLOR]attributes[COLOR=#339933].[/COLOR]add is not a [COLOR=#000000][B]function[/B][/COLOR][/FONT]
wie kann ich nach dem laden der seite einem button weitere events zuweisen, so das aber die alten noch erhalten bleiben? danke :D

#### EDIT ####
Hat sich erledgit, ich muss nur setAttribute und getAttribute nutzen XD
Code:
[FONT=monospace]tmpObjekt.getAttribute(Event)[/FONT]
 
Zuletzt bearbeitet:
ja, ich weis, das war aber wichtig für ein test! Da ich so direkt in das attribut schreiben kann!

:D und es hat funktioniert ;) mehr wollte ich garnet.
 
Das ist eine sehr umständlich Vorgehensweise. set/getAttribute ist hier keine sinnvollen Funktionen und "Funktionsname" zu übergeben auch nicht. In JS arbeitest du aber an dieser Stelle mit Funktionsreferenzen.

Da geht es darum, dass a.) this auch das Objekt ist und b.) das Eventobjekt in jedem Browser zu Verfügung steht.

PHP:
 /*
fügt einem bestimmten Objekt ein weiters Event hinzu
@param String ObjektID Die ID des HTML-Objektes
@param String Event Das Event welches erweitert werden soll (z..B: onclick)
@param String FunctionsReferenz callback Funktion welche angehangen wird 
*/
function add_event(ObjektID, Event, FunctionRef) {
   var tmpObjekt = document.getElementById(ObjektID);
   if(tmpObjekt) {
      var oldEvent = tmpObjekt[Event];
      alert(oldEvent);
      tmpObjekt[Event] = function(e) {
         if(oldEvent) oldEvent(e);
         FunctionRef(e); 
      };
   }
}
 
Status
Für weitere Antworten geschlossen.
Zurück
Oben