【问题标题】:The meaning of item in the function函数中item的含义
【发布时间】:2012-07-12 15:32:29
【问题描述】:

我从某个地方借了一段代码,但我不明白。这是一种 ajax 调用 web 服务。

function SearchMyStuff() {
$("#tblHouse").hide();
$.ajax({
    type: "POST",
    url: pageName + "SearchMyStuff",
    data: "{'oParams':" + JSON.stringify(BuildMyStuffSearch()) + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response.d.length > 0) {
            while ($('#MyStuffBody tr').length > 1) {
                $('#MyStuffBody tr:last').remove();
            }

            $.each(response.d, function (index, item) {
                var templateRow = $('#templateMyStuff');

我想知道的是

function (index, item)

这里的indexitem是什么。

谢谢你的解释。

【问题讨论】:

  • index为循环变量索引,item为当前遍历的元素

标签: javascript asp.net html json jquery


【解决方案1】:

换句话说:

$.each(array, function (index, item) { 
    //body
});

相当于:

for(var index = 0; index !== array.length; index++){
    var item = array[index];
    //body
}

当然,它更简单的相当于真正的$.each 实现

【讨论】:

  • @Love 是的,$.each() 属于 jQuery 库。
【解决方案2】:

index 值表示元素数组中的indexitem 值表示element 本身。

【讨论】:

    【解决方案3】:

    $.each 循环遍历数组中的每个元素,并使用这些参数调用回调

    index当前索引,item当前索引处的值

    【讨论】:

      猜你喜欢
      • 2019-10-08
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 2022-01-06
      相关资源
      最近更新 更多