动态更改 HTML DOM 事件

发布于 2024-08-11 13:55:58 字数 447 浏览 4 评论 0原文

我正在尝试动态更改元素的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朦胧时间 2024-08-18 13:55:58

更改

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = function() { existingFunction(i); return false; };
}

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = createOneHandler(i);
}

function createOneHandler(number){  
    return function() {  
        existingFunction(number); 
    }  
}

,它应该可以正常工作。

工作演示

这里给出了很好的解释

JavaScript,关闭 grok 的时间

Change

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = function() { existingFunction(i); return false; };
}

to

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = createOneHandler(i);
}

function createOneHandler(number){  
    return function() {  
        existingFunction(number); 
    }  
}

and it should work fine.

Working Demo

A good explanation is given here

JavaScript, time to grok closures

我ぃ本無心為│何有愛 2024-08-18 13:55:58

对于 i 始终为 4,您有范围界定问题,我建议阅读 这个。范围界定是一个非常重要的概念,因此您最好确保了解正在发生的事情。

更好的代码是

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = existingFunction;
}

onclick 会传递一个带有参数的事件,这样您就可以知道单击了哪个元素

function existingFunction(event){
  // DO something here
}

您可以在那里阅读有关事件的更多信息。 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

for (var i = 1; i < 5; i++)
{
    getElementById('element' + i).onclick = existingFunction;
}

the onclick would pass an event has argument so you can know what element have been clicked
i.e.

function existingFunction(event){
  // DO something here
}

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

煞人兵器 2024-08-18 13:55:58

您发布的代码应该按您预期的方式工作,i=4 的问题在其他地方。编辑:这是错误的,rageZ 关于范围问题是正确的。

关于另一个问题:你所能做的就是用 BTW 来减轻冗长

var f = function (i) { return function () { existingFunction(i); return false; } }
for (...) { document.getElementById(...).onclick = f(i); }

,你应该使用 jQuery 之类的东西来进行 DOM 操作(简洁的语法),也许还可以使用 Zeta (http://codex.sigpipe.cz/zeta/) 用于函数组合

var f = compose(false_, existingFunction);
for (...) { $(...).click(f(i));

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

var f = function (i) { return function () { existingFunction(i); return false; } }
for (...) { document.getElementById(...).onclick = f(i); }

BTW, you should use something like jQuery for DOM manipulation (concise syntax), and perhaps Zeta (http://codex.sigpipe.cz/zeta/) for the function composition

var f = compose(false_, existingFunction);
for (...) { $(...).click(f(i));
白衬杉格子梦 2024-08-18 13:55:58

万岁!又是闭环了!请参阅4227846435421552941 等人进行更多讨论。

有没有一种方法可以执行相同的绑定而不必每次都创建匿名函数?

是的,在 ECMAScript 第五版中,您会得到 function.bind

for (var i = 1; i < 5; i++)
    document.getElementById('element'+i).onclick= existingFunction.bind(window, i);

同时,由于浏览器尚未普遍支持它,您可以对 bind 的替代实现进行猴子修补(请参阅此评论的底部是由匿名函数构建的这样一个)作为后备。

或者,为每个元素分配相同的事件处理函数,然后让它查看 this.id 以了解它是哪个元素编号。

Hooray! It's loop closures again! See 422784, 643542, 1552941 et al for some more discussion.

is there a way of performing the same bind without having to create anonymous functions each time?

Yes, in ECMAScript Fifth Edition you get function.bind:

for (var i = 1; i < 5; i++)
    document.getElementById('element'+i).onclick= existingFunction.bind(window, i);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文