在这个简短的代码中,内联事件起作用——“事件”被传递给 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};
}
如何传递函数自己的事件?