【发布时间】:2010-09-24 11:55:23
【问题描述】:
在这段简短的代码中,内联事件起作用——“事件”被传递给 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};
}
如何传递函数自身的事件?
【问题讨论】:
标签: anonymous-function event-handling event-passthrough