jQuery/JavaScript:仅在循环的最后一次迭代期间分配事件处理程序

发布于 2024-11-07 18:36:17 字数 1701 浏览 0 评论 0原文

我遇到一个问题,即实例化两个对象的循环,这两个对象依次创建元素并附加事件处理程序,实际上仅附加循环最后一次迭代的事件处理程序。虽然我知道循环中匿名函数内部的作用域存在问题,但我看不出这在这里如何适用,而且我还是摸不着头脑……

所以,首先,视图对象的 setContent 方法会迭代内容数组,并创建 CrowdControl.ui.Button 对象的两个实例:(为了清楚起见,删除了不相关的代码)

setContent: function(content){
    if(content.length > 0){
        for(var i in content){
            var c = content[i];

            var container = $('<div></div>');

            //content array code removed for clarity

            var reject = new CrowdControl.ui.Button({label:'Reject'});
            var approve = new CrowdControl.ui.Button({label:'Approve'});

            container.append(reject.draw());
            container.append(approve.draw());

            this.contentlist.addItem(container);


        }
    }
}

这是 Button 对象(为了清楚起见,再次删除了一些代码)

CrowdControl.ui.Button = function(){
    this.uniqueId = Math.random(); //added for debugging
    var els = this.elements = {}
    els.container = $('<div class="cc-button"></div>');
    els.label = $('<div></div>').text('Debug'); 
    els.container.append(els.label);
}
CrowdControl.ui.Button.prototype.draw = function(){
        console.log('Drawing element '+this.uniqueId);
        var els = this.elements;
        els.container.remove();

        console.log('Binding click to button');
        console.log(els.container); 

        //attach the click handler - this will only attach during the last iteration
        els.container.click( function(){ console.log('wtf'); });

        return els.container;
}

上面的代码记录了所有元素的制作,它们都是唯一的,但只有 Button 对象的最后两个实例接收事件处理程序并在单击时记录“wtf”。

任何帮助将不胜感激 - 如果您需要更多代码或解释,请询问...

I'm having a problem whereby a loop which is instantiating two objects, which in turn create elements and attach event handlers, only actually attach the event handlers for the last iteration of the loop. While I'm aware of issues with scope inside anonymous functions in a loop, I can't see how that is applicable here, and I'm left scratching my head...

So, first, a setContent method of a view object iterates over an array of content, and creates two instances of the CrowdControl.ui.Button object: (irrelevant code removed for clarity)

setContent: function(content){
    if(content.length > 0){
        for(var i in content){
            var c = content[i];

            var container = $('<div></div>');

            //content array code removed for clarity

            var reject = new CrowdControl.ui.Button({label:'Reject'});
            var approve = new CrowdControl.ui.Button({label:'Approve'});

            container.append(reject.draw());
            container.append(approve.draw());

            this.contentlist.addItem(container);


        }
    }
}

Here is the Button object (again some code removed for clarity)

CrowdControl.ui.Button = function(){
    this.uniqueId = Math.random(); //added for debugging
    var els = this.elements = {}
    els.container = $('<div class="cc-button"></div>');
    els.label = $('<div></div>').text('Debug'); 
    els.container.append(els.label);
}
CrowdControl.ui.Button.prototype.draw = function(){
        console.log('Drawing element '+this.uniqueId);
        var els = this.elements;
        els.container.remove();

        console.log('Binding click to button');
        console.log(els.container); 

        //attach the click handler - this will only attach during the last iteration
        els.container.click( function(){ console.log('wtf'); });

        return els.container;
}

The above code logs that all the elements are made, and they're all unique, but only the last two instances of the Button object receive the event handlers and log 'wtf' when clicked.

Any help would be hugely appreciated - if you need any more code or explanation just ask...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我修复了您的代码: http://jsfiddle.net/maniator/E8XcQ/6/

单击应该这样做:

//attach the click handler 
$(els.container).click(function() {
    console.log('wtf');
});

您不小心将 click 附加到 DOM 元素而不是 jQuery 元素

I fixed you code: http://jsfiddle.net/maniator/E8XcQ/6/

Click should be done like this:

//attach the click handler 
$(els.container).click(function() {
    console.log('wtf');
});

You accidentally attached the click to the DOM element and not the jQuery element

云之铃。 2024-11-14 18:36:17

感谢尼尔的帮助。事实上,问题出在我框架的其他地方,每次迭代后视图都会错误地重绘,并且每次都会调用 .remove() ,这当然会从子元素中删除事件处理程序。

Thanks for the help Neal. It actually turned out the problem was elsewhere in my framework, where a view was incorrectly redrawing after each iteration, and calling .remove() each time, which was of course removing the event handlers from child elements.

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