动态添加事件 [英] Adding events dynamically

查看:66
本文介绍了动态添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单行脚本,可以将一个onunload事件处理程序添加到

文档的正文中。该脚本如下:


document.getElementsByTagName(" BODY")[0] .onunload = function s(){alert(" s")}


现在很明显,我把警报(s)部分用于调试目的,只是为了

确保错误不在我的任何代码中将要运行。这行

在IE6中运行得很好但在Firefox中却没有。但是,如果我用以下内容替换




document.getElementsByTagName(" BODY")[0] .onunload = s

我制作一个名为s的函数,然后它在两个浏览器中都运行良好。

所以,我想我只是好奇第一行是否是一个仅IE的

语法或什么。

解决方案

" David" < DK **** @ hotmail.com>在消息中写道

新闻:Ls ******************** @ comcast.com ...

我有一个单行脚本来将onunload事件处理程序添加到文档的正文中。该脚本如下:

document.getElementsByTagName(" BODY")[0] .onunload = function s()
{alert(" s")}

现在很明显,我将警报(s)部分用于调试目的,只是为了确保错误不在我将要运行的任何代码中。这个
行在IE6中运行得很好但在Firefox中却没有。但是,如果我用以下内容替换该行:

document.getElementsByTagName(" BODY")[0] .onunload = s

我制作一个名为s的函数,然后它在两个浏览器中都运行良好。
所以,我想我只是好奇第一行是否只是一个IE语言或什么。



第二个脚本行应如下所示


document.getElementsByTagName(" BODY")[0] .onunload = s( )


后括号括起来。




不,第一个例子确实是错的并且IE错误地传递了

函数对象,应该是:


obj.onunload = function(){......} //或者

obj.onunload = FUNCTIONHEREWITHNOARGS;


你也可以使用DOM obj.addEventListener()或IE obj.attachEvent();


Danny


-

使用Opera的革命性电子邮件客户端: http://www.opera.com/mail/


David写道:

" David" < DK **** @ hotmail.com>在消息中写道
新闻:Ls ******************** @ comcast.com ...

我有一个单行脚本来将onunload事件处理程序添加到文档的正文中。该脚本如下:

document.getElementsByTagName(" BODY")[0] .onunload = function s()
{alert(" s")}



该特定变体只会影响IE用户。尝试:


window.onunload = function(){alert('s''); } b / b


函数s(){

alert('s''); < br $>
}


window.onunload = s;


这两个都会惹恼IE的用户。


window.onunload是DOM级别0,不属于W3规范。 Mozilla

似乎允许动态添加正文onunload但是当页面卸载时不​​执行
执行它。在源代码中存在onunload

HTML,它似乎按预期工作。

动态添加window.onunload似乎在Mozilla和IE中工作。 />

< URL:http://www.mozilla.org/docs/dom/domref/dom_window_ref75.html>


使用onunload不鼓励一般网页,它的主要用途最初是创建''弹出地狱''。我希望

你的意图更加光荣。


[...]

-

Rob


I have a one-line script to add an onunload event handler to the body of the
document. The script is as follows:

document.getElementsByTagName("BODY")[0].onunload=function s() {alert("s")}

Now obviously, I put the alert("s") part in for debugging purposes, just to
make sure the error wasn''t in any code I was going to be running. This line
works just fine in IE6 but in Firefox it doesn''t. However, if I replace
that line with the following:

document.getElementsByTagName("BODY")[0].onunload=s

and I make a function called s, then it works just fine in both browsers.
So, I guess I''m just curious as to whether the first line is an IE-only
syntax or what.

解决方案

"David" <dk****@hotmail.com> wrote in message
news:Ls********************@comcast.com...

I have a one-line script to add an onunload event handler to the body of
the document. The script is as follows:

document.getElementsByTagName("BODY")[0].onunload=function s()
{alert("s")}

Now obviously, I put the alert("s") part in for debugging purposes, just
to make sure the error wasn''t in any code I was going to be running. This
line works just fine in IE6 but in Firefox it doesn''t. However, if I
replace that line with the following:

document.getElementsByTagName("BODY")[0].onunload=s

and I make a function called s, then it works just fine in both browsers.
So, I guess I''m just curious as to whether the first line is an IE-only
syntax or what.



That second script line should read as follows

document.getElementsByTagName("BODY")[0].onunload=s()

with the parentheses after s.



No, the 1st example is indeed mistaken and IE is incorrectly passing the
function object, should be:

obj.onunload=function(){ ...... } // or
obj.onunload=FUNCTIONHEREWITHNOARGS;

You can also use DOM obj.addEventListener() or for IE obj.attachEvent();

Danny

--
Using Opera''s revolutionary e-mail client: http://www.opera.com/mail/


David wrote:

"David" <dk****@hotmail.com> wrote in message
news:Ls********************@comcast.com...

I have a one-line script to add an onunload event handler to the body of
the document. The script is as follows:

document.getElementsByTagName("BODY")[0].onunload=function s()
{alert("s")}


That particular variant will pester IE users only. Try:

window.onunload = function () { alert(''s''); };

or

function s (){
alert(''s'');
}

window.onunload = s;

Both of which will annoy users of IE also.

window.onunload is DOM level 0 and not part of the W3 spec. Mozilla
seems to allow the body onunload to be added dynamically but doesn''t
execute it when the page unloads. Where onunload exists in the source
HTML, it seems to work as expected.

Adding window.onunload dynamically seems to work in Mozilla and IE.

<URL:http://www.mozilla.org/docs/dom/domref/dom_window_ref75.html>

The use of onunload is not encouraged for general web pages, its
principal use initially having been to create ''pop-up hell''. I hope
your intentions are more honourable.

[...]
--
Rob


这篇关于动态添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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