Pass additional parameters to jQuery each() callback

后端 未结 4 692
梦谈多话
梦谈多话 2020-12-24 05:32

I\'m working on an app that will present surveys to the user. The markup looks something like this:


    
4条回答
  •  無奈伤痛
    2020-12-24 06:09

    //Create extra data
    var dataArray = ["A", "B", "C"];
    
    //Send default arguments from jQuery's each (index and item) along
    //with our new data argument to a named function handler.
    $("#myList").children().each(function(jqIndex, jqItem)
                                {
                                  listItemsHandler(jqIndex, jqItem, dataArray[jqIndex]);
                                });    
    
    //Named function handler accepting 3 arguments.
    function listItemsHandler(index, item, data)
    {
        console.log(index + " " + item + " " + data);
    }
    
    
    
    1. First
    2. Second
    3. Third

提交回复
热议问题