简体   繁体   English

带有参数的jQuery click事件不起作用

[英]jQuery click event with parameter not working

I need some help. 我需要协助。 I am trying to iterate through some Json items, create some li's with some information from the items, but also attach click handlers for the li's, but with a value transmited as parameter. 我试图遍历一些Json项,使用项中的一些信息创建一些li,还附加了li的单击处理程序,但是将值作为参数传输。

The problem is that the last value of that parameter is set for or the li's in the list. 问题是该参数的最后一个值被设置为或列表中的li。 From what i searched i understood that it has something to do with javascript closure, but can't seem to understand how to fix it. 从我的搜索中,我了解到它与javascript闭包有关,但似乎无法理解如何解决它。

I am using jQuery and here is the code: 我正在使用jQuery,这是代码:

for (var i = 0; i < items.length; i++)
{
    // information that will be displayed for each video
    var entry = items[i];

    var title = entry.title;
    var image = entry.thumbnail.hqDefault;
    var id = entry.id;

    var li = $("<li class='video-single'>");
    li.append("<img src='" + image + "' alt='" + title + "'>");
    li.append("<h4>" + title + "</h4>");

    $(li).click(function() {
        displayPopUp(id);
    });

    ul.append(li);
}

Could anyone please help me fix this code? 有人可以帮我解决此代码吗?

Best regards, Marius. 最好的问候,马吕斯。

The issue is that JS is function scoped so the id within your closure is the last id from your for loop. 问题在于JS是函数作用域的,因此闭包中的id是for循环中的最后一个id。 To fix this use forEach so you run each iteration of your loop in a separate scope eg 为了解决这个问题,您可以在单独的范围内运行循环的每次迭代,例如

items.forEach(function (el, i) {
    // information that will be displayed for each video
    var entry = items[i];

    var title = entry.title;
    var image = entry.thumbnail.hqDefault;
    var id = entry.id;

    var li = $("<li class='video-single'>");
    li.append("<img src='" + image + "' alt='" + title + "'>");
    li.append("<h4>" + title + "</h4>");

    $(li).click(function() {
        displayPopUp(id);
    });

    ul.append(li);
});

you need delegated event as elements are created dynamically, you can use class that is being added on li which is video-single : 由于元素是动态创建的,因此需要委托事件 ,可以使用在li上添加的类video-single

$(document).on('click','.video-single',function() {
                        displayPopUp(id);
                    });

you can read about Delegated Events HERE 您可以在此处了解有关委托活动的信息

in order to bind an event to a dynamically added element you need to delegate it as below: 为了将事件绑定到动态添加的元素,您需要按以下方式委派它:

for (var i = 0; i < items.length; i++)
            {
                // information that will be displayed for each video
                var entry = items[i];

                var title = entry.title;
                var image = entry.thumbnail.hqDefault;
                var id = entry.id;

                var li = $("<li class='video-single'>");
                li.append("<img src='" + image + "' alt='" + title + "'>");
                li.append("<h4>" + title + "</h4>");

                $(document).bind('click',li,function() {
                    displayPopUp(id);
                });

                ul.append(li);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM