2008-11-28 12 views
0

Dans ce code abrégé, l'événement en ligne fonctionne - le « événement » est passé à la fonction testKeyPressFirefox: passer l'événement de l'anonymat fonction javascript

<textarea id="source" 
    onkeydown= "showCursPos(this); 
    var tf=testKeyPress(event); 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; " ></textarea> 

function testKeyPress(e){ 
    if (e.ctrlKey==true){ 
     if (e.which == null){kcode=e.keyCode; } // IE 
     else if (e.which > 0){kcode=e.which; } // gecko 
     return testValidity(kcode); //returns true-false 
    } 
} 

Cependant, dans cette version anonyme, l'événement ne passe dans gecko:

<textarea id="source"></textarea> 

$("source").onkeydown = function(){ 
    showCursPos(this); // this function works 
    // next event is passed in IE, but not gecko 
    var tf=testKeyPress(event); 
    // remaining functions work if value is forced 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
    } 

Comment passer l'événement de la fonction?

Répondre

3

Oui, il existe un objet événement en tant qu'arguments.

Vous pouvez l'obtenir par

var e=arguments[0] || event; // Firefox via the argument, but IE don't 

Je ne sais pas si elles exactes même, mais je lis <xxx onkeydown="func(event);"> comme xxx.ononkeydown=function(event){func(event);};

Référence event @ Mozilla.org

+0

Votre solution a fonctionné pour moi. :-) –

0

travaillé comme un charme. Cette modification de mon code original passe avec succès l'événement de la fonction anonyme à la fonction nommée dans mes quatre navigateurs: IE6, FF2, NS7.2, OP9.22

$("source").onkeydown = function(){ 
    var e=arguments[0] || event; 
    showCursPos(this); 
    var tf=testKeyPress(e); 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
}