在IE中向对象添加动态事件!? [英] Adding dynamic events to objects in IE!?

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

问题描述




好​​的,这是我的问题...


我正在动态构建一些内容(类似这个):


var fooObj = document.createElement(''div'');

fooObj.setAttribute(''id'','' uniqueNumber'');





我有的问题是,我想做的(与此类似):


fooObj.setAttribute(''onclick'',''func1(); func2(''+ some.dyn.reference +

''); return false;'');

当然,它的工作方式就像butta,在* Mozilla *和* Opera * ......但_IE_

只是默默地忽略它.. 。


-------------------


现在,我知道我可以使用:


fooObj.click = nameOfFuncWithNoQuotesOrBrackets;


但是,我只限于1(一)功能,*更多*重要的是,我不能将
传递给参数函数......(IIRC)


----------- --------


寻找想法...... ;-)


请记住......


a。)I *有*动态创建这个元素(它直到

运行时才存在)

b。)我*有*能够传递参数(s )

c。)*理想情况下*我希望能够连续几次调用

语句(例如js stmts / functions / returns的任何组合,但是

如果我可以用参数调用一个函数,我*假设*我可以使那个

函数依次调用我需要的一个。


d。)虽然我真的希望这个在IE中工作,如果它变成了

,它可以' 好吧,那也没关系...这可能是这个迷你应用程序支持这个浏览器的最后一根

(阅读:在IE中调试就像用电锯剃须......你*可以*

这样做,但为什么要尝试!)


干杯,

史蒂夫

解决方案

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



好的,这是我的问题...

我正在动态构建一些内容(类似于此):

var fooObj = document.createElement(''div'');
fooObj.setA ttribute(''id'',''uniqueNumber'');


我很确定如果你搜索这个新闻组,你会找到一个解决方案

,但见下文。



我遇到的问题是,我想做(与此类似):

fooObj.setAttribute(''onclick'',''func1(); func2(''+ some.dyn.reference +
''); return false;'');


fooObj.onclick = function(){

//使用参数的语句

};


这会创建一个匿名函数,请注意闭包[1]。你还可以创建一个添加onclick的函数,并将你的

参数传递给它:


var fooObj = document .createElement(''div'');

addOnclick(fooObj,parm1,parm2);

...

}


函数addOnclick(obj,p1,p2)

{

obj.onclick = function(){

//使用p1和p2的语句

}

}


第二种方法可以解决一些关闭问题。


在第一种方法中,当onclick运行时评估参数

如果你希望它们有点类似于全局变量,它们很方便 - 它们的价值
除了通过onlclick,
可能会发生变化。在第二种方法中,

参数传递给addOnclick并在添加到

元素时进行评估,因此它们不应该更改。


1.参见常见问题解答注意事项:


< URL:http://jibbering.com/faq/faq_notes/closures.html>


2.以下是一些可能感兴趣的主题:


< URL:http://groups.google.co.uk/group/comp.lang.javascript ?/ browse_frm /线程/ fbf6b4f6c9dc1806 / 96b2393ad65dd1aa q =动态+ +添加的onclick&安培; RNUM = 9&安培; hl = en#96b2393ad65dd1aa>

< URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/b58ee83c15103497/bcbd2eaa0ffa3c1d?q =动态+添加+ onclick& rnum = 13& hl = en#bcbd2eaa0ffa3c1d>

[...]请记住......

a。)我*有*动态创建这个元素(它直到
运行时才存在)


不成问题

b。)I *有*能够传递参数


不是问题

c。)*理想情况下*我希望能连续几次打电话
语句(例如js stmts / functions / returns的任何组合),但是如果我可以使用参数调用一个函数,我*假设*我可以使那个
函数依次调用一个(s)我需要。


你可以添加多个功能,如果你愿意,有很多参数

d。)尽管我真的希望这个在IE中工作,如果它转过来说它不可能,那也没关系......这可能是支持这个迷你应用程序浏览器的最后一根稻草。
(阅读:IE中的调试就像用电锯剃须......你*可以*
去做,但为什么要尝试!)


以上也将在IE中工作。对于IE有合理的调试解决方案

,有些需要MS Office或Visual Studio来获取它们(搜索

新闻组,最近一系列有关调试的帖子IE)。

这里是一个:


< URL:http://groups.google.co.uk/group/comp.lang .javascript / browse_frm / thread / 4e1ef68ed8443aff / 8c4dd4a80dafb832?q = IE + debugging +& rnum = 13& hl = en#8c4 dd4a80dafb832>

干杯,
Steve



-

Rob


RobG写道:

var fooObj = document.createElement (''div'');
addOnclick(fooObj,parm1,parm2);
...


函数addOnclick(obj,p1,p2)
{
obj.onclick = function(){
//使用p1和p2的语句
}


第二种方法可能解决一些关闭问题。




这不会创造一个记忆在IE中也有泄漏?

带有事件处理程序的DOM节点现在在同一范围内,创建了一个

循环引用。我以为.....

这就是为什么我总是这样做:


var fooObj = document.createElement(''div'' );

fooObj.onclick = addOnclick(parm1,parm2);

...

}


函数addOnclick(p1,p2)

{

return = function(){

//使用p1和p2
}

}


罗伯特。


我听说过很多关于这个循环参考的信息。与DOM ...你能否更好地解释这个范围/内存泄漏问题?


-Brendan


Hi,

Okay, here''s my problem...

I''m dynamically building some content (similar to this):

var fooObj = document.createElement(''div'');
fooObj.setAttribute(''id'', ''uniqueNumber'');

etc.

The issue I have, is that I want to do (similar to this):

fooObj.setAttribute(''onclick'', ''func1();func2('' + some.dyn.reference +
'');return false;'');
Of course, it works like butta, in *Mozilla*, and *Opera*... but _IE_
just quietly ignores it...

-------------------

Now, I know I can use:

fooObj.click = nameOfFuncWithNoQuotesOrBrackets;

but, I''m limited to 1 (one) function, and *more* importantly, I can''t
pass a parameter to the function... (IIRC)

-------------------

Looking for ideas... ;-)

Please keep in mind though...

a.) I *have* to dynamically create this element (it doesn''t exist until
run-time)
b.) I *have* to be able to pass parameter(s)
c.) *Ideally* I would like to be able to call several consecutive
statements (e.g. any combo of js stmts / functions / returns), however
if I can call one function with parameters, I *suppose* I can make that
function in turn call the one(s) I need.

d.) Allthough I would really like this to work in IE too, if it turns
out that it can''t, well, that''s okay too... This would likely be the
''official'' last straw in supporting this Browser for this mini-app.
(read: Debugging in IE is like shaving with a chainsaw... you *could*
do it, but why even try!)

Cheers,
Steve

解决方案

st**************@gmail.com wrote:

Hi,

Okay, here''s my problem...

I''m dynamically building some content (similar to this):

var fooObj = document.createElement(''div'');
fooObj.setAttribute(''id'', ''uniqueNumber'');

I''m pretty sure if you search this newsgroup you''ll find a solution to
that, but see below.
etc.

The issue I have, is that I want to do (similar to this):

fooObj.setAttribute(''onclick'', ''func1();func2('' + some.dyn.reference +
'');return false;'');
fooObj.onclick = function() {
// statements using parameters
};

This creates an anonymous function, be careful with closures[1]. You
can also create a function that adds the onclick and you pass your
parameters to it:

var fooObj = document.createElement(''div'');
addOnclick(fooObj, parm1, parm2);
...
}

function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The second method may solve some closure issues.

In the first method, the parameters are evaluated when the onclick runs
which is handy if you want them to be sort of like globals - their value
may change other than through the onlclick. In the second method,
parameters are passed to addOnclick and evaluated when added to the
element so they should not change.

1. See the group FAQ notes:

<URL:http://jibbering.com/faq/faq_notes/closures.html>

2. Here are some threads that may be of interest:

<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/fbf6b4f6c9dc1806/96b2393ad65dd1aa?q=dynamically+add+onclick&rnum=9& hl=en#96b2393ad65dd1aa>
<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/b58ee83c15103497/bcbd2eaa0ffa3c1d?q=dynamically+add+onclick&rnum=13 &hl=en#bcbd2eaa0ffa3c1d>
[...] Please keep in mind though...

a.) I *have* to dynamically create this element (it doesn''t exist until
run-time)
Not a problem
b.) I *have* to be able to pass parameter(s)
Not a problem
c.) *Ideally* I would like to be able to call several consecutive
statements (e.g. any combo of js stmts / functions / returns), however
if I can call one function with parameters, I *suppose* I can make that
function in turn call the one(s) I need.
You can add multiple functions if you like, with lots of parameters

d.) Allthough I would really like this to work in IE too, if it turns
out that it can''t, well, that''s okay too... This would likely be the
''official'' last straw in supporting this Browser for this mini-app.
(read: Debugging in IE is like shaving with a chainsaw... you *could*
do it, but why even try!)
The above will work in IE too. There are reasonable debugging solutions
for IE, some require MS Office or Visual Studio to get them (search the
newsgroup, there''s a recent series of posts regarding debugging in IE).
Here''s one:

<URL:http://groups.google.co.uk/group/comp.lang.javascript/browse_frm/thread/4e1ef68ed8443aff/8c4dd4a80dafb832?q=IE+debugging+&rnum=13&hl=en#8c4 dd4a80dafb832>

Cheers,
Steve


--
Rob


RobG wrote:

var fooObj = document.createElement(''div'');
addOnclick(fooObj, parm1, parm2);
...
}

function addOnclick(obj, p1, p2)
{
obj.onclick = function() {
// Statements using p1 and p2
}
}

The second method may solve some closure issues.



Doesn''t this create a memory leak in IE too?
The DOM Node with the event handler is now in the same scope, creating a
circular reference. I thought.....
That''s why I always do something like:

var fooObj = document.createElement(''div'');
fooObj.onclick = addOnclick(parm1, parm2);
...
}

function addOnclick(p1, p2)
{
return = function() {
// Statements using p1 and p2
}
}

Robert.


I''ve heard a lot about this "circular reference" with the DOM...can you
more clearly explain this scope / mem leak issue?

-Brendan


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

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