动态更改 HTML DOM 事件
我正在尝试动态更改元素的 onClick 事件,并且有类似以下内容的内容:
for (var i = 1; i < 5; i++)
{
getElementById('element' + i).onclick = function() { existingFunction(i); return false; };
}
除了每次调用时传递给 'existingFunction()' 的参数是 i=4 的最终值这一事实之外,一切似乎都工作正常。有没有一种方法可以将函数绑定到 onclick,该函数在绑定时使用 i 的值,而不是目前看起来正在执行的操作并在 for 循环中引用原始 i 。
还有一种方法可以执行相同的绑定,而不必每次都创建匿名函数?这样我就可以出于性能原因在每个 onclick 中直接引用“existingFunction”?
干杯,伙计们, 勇
I am trying to dynamically change an element's onClick event and I have something like the following:
for (var i = 1; i < 5; i++)
{
getElementById('element' + i).onclick = function() { existingFunction(i); return false; };
}
Everything seems to work fine apart from the fact that the argument passed to 'existingFunction()' is the final value of i=4 each time it is called. Is there a way to bind a function to onclick that uses the value of i at the time of binding as opposed to what it seems to be doing at the moment and referencing the original i in the for-loop.
Also is is there a way of performing the same bind without having to create anonymous functions each time? so that I can directly reference 'existingFunction' in each onclick for performance reasons?
Cheers guys,
Yong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改
为
,它应该可以正常工作。
工作演示
这里给出了很好的解释
JavaScript,关闭 grok 的时间
Change
to
and it should work fine.
Working Demo
A good explanation is given here
JavaScript, time to grok closures
对于
i
始终为 4,您有范围界定问题,我建议阅读 这个。范围界定是一个非常重要的概念,因此您最好确保了解正在发生的事情。更好的代码是
onclick 会传递一个带有参数的事件,这样您就可以知道单击了哪个元素
即
您可以在那里阅读有关事件的更多信息。 IE 确实具有与其他浏览器完全相同的事件模型,因此您必须处理它。
最后一点,我建议你使用 JS 框架(Jquery、ExtJS、DOJO、Prototype...),因为它会简化你的任务
for the
i
being always 4, you have a scoping problem, I advise to read this. Scoping is are really important concept, so you have better to make sure to understand what's is going on.a better code would be
the onclick would pass an event has argument so you can know what element have been clicked
i.e.
you can read more about events there. IE does have the exact same event model as other browser so you would have to handle it.
Last bit, I advise you to use a JS framework(Jquery,ExtJS,DOJO,Prototype...) because it would simplify your task
您发布的代码应该按您预期的方式工作,i=4 的问题在其他地方。编辑:这是错误的,rageZ 关于范围问题是正确的。
关于另一个问题:你所能做的就是用 BTW 来减轻冗长
,你应该使用 jQuery 之类的东西来进行 DOM 操作(简洁的语法),也许还可以使用 Zeta (http://codex.sigpipe.cz/zeta/) 用于函数组合
the code you posted should work the way you intended, your problem with i=4 is elsewhere. edit: this is wrong, rageZ is right about the scoping problem.
re the other question: all you can do is offload the verbosity with
BTW, you should use something like jQuery for DOM manipulation (concise syntax), and perhaps Zeta (http://codex.sigpipe.cz/zeta/) for the function composition
万岁!又是闭环了!请参阅422784、643542,1552941 等人进行更多讨论。
是的,在 ECMAScript 第五版中,您会得到
function.bind
:同时,由于浏览器尚未普遍支持它,您可以对
bind
的替代实现进行猴子修补(请参阅此评论的底部是由匿名函数构建的这样一个)作为后备。或者,为每个元素分配相同的事件处理函数,然后让它查看 this.id 以了解它是哪个元素编号。
Hooray! It's loop closures again! See 422784, 643542, 1552941 et al for some more discussion.
Yes, in ECMAScript Fifth Edition you get
function.bind
:In the meantime since browsers don't yet generally support it you can monkey-patch an alternative implementation of
bind
(see the bottom of this comment for one such) built out of anonymous functions as a fallback.Alternatively, assign the same event handler function to every element and just have it look at
this.id
to see which element number it is.