Firefox:从匿名 JavaScript 函数传递事件
在这段简短的代码中,内联事件起作用了 - “事件”被传递给 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};
}
如何传递函数自己的事件?
In this abbreviated code, the inline event works - the "event" is passed to the testKeyPress function
<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
}
}
However, in this anonymous version, the event is not passed in 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};
}
How does one pass the function's own event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,有一个事件对象作为参数。
您可以通过
我不知道它们是否完全相同,但我将
读为xxx.ononkeydown=function( event){func(event);};
参考 event @ Mozilla.org
Yes, there is an event object as arguments.
You can get it by
I don't know if they exact the same, but I read
<xxx onkeydown="func(event);">
asxxx.ononkeydown=function(event){func(event);};
Reference event @ Mozilla.org
工作起来就像一个魅力。 对我的原始代码的修改成功地将事件从匿名函数传递到我的四个浏览器中的命名函数:IE6、FF2、NS7.2、OP9.22
Worked like a charm. This modification to my original code successfully passes the event from the anonymous function to the named function in my four browsers: IE6, FF2, NS7.2, OP9.22