为什么“事件”没有在Mozilla中定义 [英] Why 'event' is not defined in Mozilla

查看:88
本文介绍了为什么“事件”没有在Mozilla中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有人可以解释,为什么在下面的代码中,我得到事件未定义

错误

funcTest(sMenu)

{

doument.getElementById(''id1'')。addEventListener(''mo usedown'',function(){

点击(sMenu,event);},false);

}

但同时代码如下工作就好了/>

< input type =" text"名称= QUOT;小工具" ID = QUOT;小工具"大小= QUOT; 2英寸value =" 0"

onchange =" funcChg();"


onkeypress =" return isNumberInput(this,event);" />


谢谢

Prabh

Hi,

Can somebody explain, why in following code, i get "event not defined"
error

funcTest(sMenu)
{
doument.getElementById(''id1'').addEventListener(''mo usedown'', function(){
click(sMenu, event); }, false);
}
But at the same time code as following works just fine

<input type="text" name="gadgets" id="gadgets" size="2" value="0"
onchange="funcChg();"

onkeypress="return isNumberInput(this, event);" />

Thanks
Prabh

推荐答案

pr*******@gmail.com 写道:
pr*******@gmail.com wrote:
有人可以解释,为什么在下面的代码中,我得到事件未定义
错误

funcTest(sMenu)
^^^^^^^^ [1] {
doument.getElementById(''id1'')。addEventListener(''mo usedown'',function(){
[2] ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [3] [4] ^^^^^^^^^^^点击(sMenu,event);},false);
^^^^^^^^^^^^^^^^^^^^^^ [4]}

但同时代码如下工作正常

< input type =" text"名称= QUOT;小工具" ID = QUOT;小工具"大小= QUOT; 2英寸值= QUOT; 0"
^^^^^^^^^^^ [5] onchange =" funcChg();"
^^^^^^^^^ [6]
onkeypress =" return isNumberInput(this,event);" />
Can somebody explain, why in following code, i get "event not defined"
error

funcTest(sMenu) ^^^^^^^^[1] {
doument.getElementById(''id1'').addEventListener(''mo usedown'', function(){ [2]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[3][4] ^^^^^^^^^^^ click(sMenu, event); }, false); ^^^^^^^^^^^^^^^^^^^^^^[4] }
But at the same time code as following works just fine

<input type="text" name="gadgets" id="gadgets" size="2" value="0" ^^^^^^^^^^^[5] onchange="funcChg();" ^^^^^^^^^[6]
onkeypress="return isNumberInput(this, event);" />



^^^^^^^^^^^^^^ [7] ^ [8]

[1]没有用户定义的funcTest()方法可能导致

问题,因为

FunctionDeclaration或FunctionExpression缺少保留字function。


[2]在任何已知的DOM中没有doument引用,并且没有

这里是用户定义的。


[3]这里没有ID为id1的元素。


[4]你没有进行任何功能测试。


[5]`type =" text"''对于`input''元素是可选的。


[6]没有内置的funcChg()方法,没有用户 - 这里定义。


[7]这里没有内置的isNumberInput(),也没有用户自定义。


[8] XHTML Internet Explorer不支持,并将XHTML作为

text / html发送,以实现无法实现的HTML兼容性。根据

到XHTML 1.0,附录C,被认为是有害的。


< URL:http://jibbering.com/faq/#FAQ4_43>

< URL:http://www.8pointedears.de/scripts/test/whatami>

< URL:http://hixie.ch/advocacy/xhtml> ;

< URL:http://validator.w3.org>

< URL:http://jibbering.com/faq/>

PointedEars


^^^^^^^^^^^^^^[7] ^[8]
[1] There is no user-defined funcTest() method probably causing a
problem here as the reserved word `function'' is missing for a
FunctionDeclaration or FunctionExpression.

[2] There is no `doument'' reference in any known DOM and it is none
user-defined here.

[3] There is no element with ID `id1'' here.

[4] You do not feature-test anything.

[5] `type="text"'' is optional for `input'' elements.

[6] There is no built-in funcChg() method and none user-defined here.

[7] There is no built-in isNumberInput() and none user-defined here.

[8] XHTML is not supported by Internet Explorer, and sending XHTML as
text/html to achieve non-achievable "HTML compatibility" according
to XHTML 1.0, Appendix C, is considered harmful.

<URL:http://jibbering.com/faq/#FAQ4_43>
<URL:http://www.pointedears.de/scripts/test/whatami>
<URL:http://hixie.ch/advocacy/xhtml>
<URL:http://validator.w3.org>
<URL:http://jibbering.com/faq/>
PointedEars


2005年11月25日13:55, pr ******* @ gmail.com 写道:
On 25/11/2005 13:55, pr*******@gmail.com wrote:
有人可以解释,为什么在下面的代码中,我得到事件未定义
错误


Thomas对许多问题发表了评论,但忽略了这个特定问题的原因。可能是疏忽。


[snip]

doument.getElementById(''id1'')。addEventListener(''mo usedown'',function( ){
click(sMenu,event);},false);


由Mozilla和其他人(不包括

IE)实现的W3C事件模型没有定义全局事件对象。而是将对象传递给听众。


var元素;


if(document.getElementById

&&(element = document.getElementById(''id1''))

&& element.addEventListener)

{

element.addEventListener(''mousedown'',函数(事件){

click(sMenu,event);

},false) ;

}


注意在函数表达式中添加了一个参数。


据推测,click是a用户定义的函数。

但同时代码如下工作就好了


[snip]

onkeypress =" ; return isNumberInput(this,event);" />
Can somebody explain, why in following code, i get "event not defined"
error
Thomas commented on many problems, though omitted the cause of this
specific issue. An oversight, probably.

[snip]
doument.getElementById(''id1'').addEventListener(''mo usedown'', function(){
click(sMenu, event); }, false);
The W3C event model, as implemented by Mozilla and others (excluding
IE), does not define a global event object. Instead, the object is
passed to listeners.

var element;

if(document.getElementById
&& (element = document.getElementById(''id1''))
&& element.addEventListener)
{
element.addEventListener(''mousedown'', function(event) {
click(sMenu, event);
}, false);
}

Notice the addition of an argument in the function expression.

Presumably, click is a user-defined function.
But at the same time code as following works just fine
[snip]
onkeypress="return isNumberInput(this, event);" />




当浏览器遇到内部事件属性时,该

属性的值实际上成为创建的函数对象的主体<浏览器内部为
.


在IE中,这种转换类似于:


element.onkeypress = function (){

返回isNumberInput(this,event);

};


因为它定义了一个全局事件对象, ''event''标识符将解析

到该全局属性。


在实现W3C事件模型的浏览器中,转换是

类似于我之前发布的函数表达式:


element.onkeypress = function(event){

return isNumberInput(this,event);

};


通过这种方式,两个模型明显不同,但可互操作

(在本例中)。 />

迈克


-

Michael Winter

在通过电子邮件回复之前使用[新闻]作为前缀主题。



When a browser encounters intrinsic event attributes, the value of that
attribute effectively becomes the body of a function object created
internally by the browser.

In IE, this transformation is similar to:

element.onkeypress = function() {
return isNumberInput(this, event);
};

As it defines a global event object, the ''event'' identifier will resolve
to that global property.

In browsers that implement the W3C event model, the transformation is
similar to the function expression I posted earlier:

element.onkeypress = function(event) {
return isNumberInput(this, event);
};

In this way, the two models are markedly different, but interoperable
(in this instance).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


pr ******* @ gmail.com 写道:


有人可以解释,为什么在下面的代码中,我得到 ;事件没有定义
错误


除了所有语法问题,我会尝试解释你遇到的问题

(我认为) )。

< input type =" text"名称= QUOT;小工具" ID = QUOT;小工具"大小= QUOT; 2英寸value =" 0"
onchange =" funcChg();"

onkeypress =" return isNumberInput(this,event);" />


当你在事件处理程序(例如onkeypress)时,有一个

隐式事件对象。

你可以看到在下一个例子中它更好


< script type =" text / javascript">

function test1(event)

{

alert(event.target);

}

< / script>

< button id =" btn"> Button< / button>

< script type =" text / javascript">

document.getElementById(" btn" ;)。onclick = test1;

< / script>

Mozilla(以及其他符合标准的浏览器)传递事件对象

作为第一个参数,即使你没有明确地传递这个参数



Internet Explorer也不会这样做。但是它可以访问窗口对象中的全局

事件对象。

上面的代码在Internet Explorer中不起作用,因为事件

参数将是未定义的。

因此,为了使它在Mozilla和IE中都能正常工作,你会经常看到这样的代码




function test1(event)

{

event = event || window.event;

alert(event.target);

}


首先看到是否定义了事件参数如果没有,它将使用全局事件对象。

funcTest(sMenu)
{doument.getElementById(''id1'')。 addEventListener(''mo usedown'',function(){
click(sMenu,event);},false);
}
Hi,

Can somebody explain, why in following code, i get "event not defined"
error
Beside all the syntax problems I will try to explain what is the problem
you are having (I think).
<input type="text" name="gadgets" id="gadgets" size="2" value="0"
onchange="funcChg();"

onkeypress="return isNumberInput(this, event);" />
When you are in an event handler (such as onkeypress) there is an
implicit event object.
You can see it better in the next example

<script type="text/javascript">
function test1(event)
{
alert(event.target);
}
</script>
<button id="btn">Button</button>
<script type="text/javascript">
document.getElementById("btn").onclick = test1;
</script>

Mozilla (and other standards compliant browsers) passes an event object
as the first parameter even though you did not pass this parameter
yourself explicitly.
Internet Explorer doesn''t do this. However it has access to a global
event object in the window object.
The code above will not work in Internet Explorer, because the event
parameter will be undefined.
Therefore to make it work in both Mozilla and IE you will often see code
like this:

function test1(event)
{
event = event || window.event;
alert(event.target);
}

which first sees if the event parameter is defined and if not it will
use the global event object.

funcTest(sMenu)
{
doument.getElementById(''id1'').addEventListener(''mo usedown'', function(){
click(sMenu, event); }, false);
}




在这个例子中,Mozila的范围内根本没有事件对象。

Internet Explorer使用全局事件对象。

为了完成这项工作,你可以这样做:


函数funcTest(event,sMenu)

{

doument.getElementById(''id1'')。addEventListener('' mo usedown'',function(){

click(sMenu,event);},false);

}


你必须在调用funcTest时自己传递事件对象。



In this example there simply is no event object in scope for Mozila.
Internet Explorer uses the global event object.
To make this work your could do something like this:

function funcTest(event, sMenu)
{
doument.getElementById(''id1'').addEventListener(''mo usedown'', function(){
click(sMenu, event); }, false);
}

And you have to pass the event object yourself when calling funcTest.


这篇关于为什么“事件”没有在Mozilla中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆