0

この省略されたコードでは、インライン イベントが機能します。「イベント」は testKeyPress 関数に渡されます。

<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
    }
}

ただし、この匿名バージョンでは、イベントは 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}; 
    }

関数自体のイベントをどのように渡すのですか?

4

2 に答える 2

3

はい、引数としてイベント オブジェクトがあります。

あなたはそれを得ることができます

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

それらがまったく同じかどうかはわかりませんが、私は次のように読み<xxx onkeydown="func(event);">ましたxxx.ononkeydown=function(event){func(event);};

参照イベント @ Mozilla.org

于 2008-11-28T19:15:04.217 に答える
0

魅力のように働きました。元のコードをこのように変更すると、IE6、FF2、NS7.2、OP9.22 の 4 つのブラウザーで、無名関数から名前付き関数にイベントが正常に渡されます。

$("source").onkeydown = function(){ 
    var e=arguments[0] || event; 
    showCursPos(this);  
    var tf=testKeyPress(e); 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
}
于 2008-11-28T21:02:48.070 に答える