【问题标题】:How to build a ul li dynamically from two arrays - Jquery如何从两个数组动态构建 ul li - Jquery
【发布时间】:2014-04-25 06:18:01
【问题描述】:

我得从两个数组动态构建一个轮播,如下:

var arrayBarcode = [123456789, 234567891, 345678912];
var arrayPath = [/assets/image1.jpg, /assets/image2.jpg,/assets/image3.jpg];

首先,我只有 div 类 flexslider

我需要构建我的 ul li,以便我的最终 html 输出以下内容:

<div class="flexslider">

<ul class="slides">

    <li ean="123456789">

        <img src="/assets/image1.jpg" alt="pix" draggable="false">

        <a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons" title="Remove" href="#">
            <i aria-hidden="true" class="icon icon-close-neg">
                <span class="sr-only">Remove</span>
            </i>
        </a>

    </li>   

    <li ean="234567891">

        <img src="/assets/image2.jpg" alt="pix" draggable="false">

        <a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons" title="Remove" href="#">
            <i aria-hidden="true" class="icon icon-close-neg">
                <span class="sr-only">Remove</span>
            </i>
        </a>

    </li>

    <li ean="345678912">

        <img src="/assets/image3.jpg" alt="pix" draggable="false">

        <a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons" title="Remove" href="#">
            <i aria-hidden="true" class="icon icon-close-neg">
                <span class="sr-only">Remove</span>
            </i>
        </a>

    </li>

</ul>

请注意,arrayBarcode 中的第一项与 arrayPath 中的第一项匹配,等等。

任何帮助将不胜感激..

谢谢..

【问题讨论】:

  • 您尝试过任何代码吗?
  • 你有没有尝试过?给我们一些代码示例。如果还没有,您可以尝试循环遍历数组并在它们两个上选择相同的索引。但是,如果两个数组上的元素不匹配,这将中断。
  • 我正在按如下方式构建数组: var thecookie = $.cookie(COOKIETOPRINT); var cookies = thecookie.split("|"); cookies.forEach(function(item){ var barcode= item.split('~')[0]; var path = item.split('~')[2]; arrEan.push(barcode); arrPath.push(路径);
  • 我尝试遵循与stackoverflow.com/questions/5881033/… 相同的逻辑,但没有成功:(

标签: javascript jquery


【解决方案1】:

遍历 JavaScript 数组非常简单。考虑访问数组的每个索引并分配它们或更好的方法,使用类 slides 附加到我们的 UL 第一个答案很好,但还不够,因为对数组使用 FOR-IN 是一种不好的做法,当然您可以选择将所有 List Items 放在一个局部变量中(如 @ 987654321@ 的示例)或单独附加。

这是示例代码,未经测试:

    var arrayBarcode = [123456789, 234567891, 345678912],
        arrayPath = [/assets/image1.jpg, /assets/image2.jpg,/assets/image3.jpg],
    $slides = $('.slides'); // If only one single class available on your HTML Doc
    if (arrayBarCode.length != arrayPath.length)
    {
     console.log('Both array should be the same size');
     return;
    }

    for (var i = 0; i < arrayBarcode.length; i++)
    {
     var html= '<li ean="' + arrayBarcode[i] + '">\
        <img src="' + arrayPath[i] + '" alt="pix" draggable="false">\
        <a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons" title="Remove" href="#">\
            <i aria-hidden="true" class="icon icon-close-neg">\
                <span class="sr-only">Remove</span>\
            </i>\
        </a>\
    </li>';
    $slides.append(html);
}

【讨论】:

    【解决方案2】:

    大致如下:

    $('div.flexslider').append('<ul class="slides"></ul>');
    var aElement = $('<a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons" title="Remove" href="#">
            <i aria-hidden="true" class="icon icon-close-neg">
                <span class="sr-only">Remove</span>
            </i>
        </a>');
    
    $.each(arrayBarcode, function(index, value){
         var liElement = $('<li></li>').attr('ean', value);
         var imgElement = $('<img alt="pix" draggable="false">').attr('src', arrayPath[index]);
         liElement.append(imgElement).append(aElement.clone());
         $('ui.slides').append(liElement);
    });
    

    另外,数组arrayPath缺少“”:

    var arrayPath = ['/assets/image1.jpg', '/assets/image2.jpg','/assets/image3.jpg'];
    

    【讨论】:

      【解决方案3】:

      试试这个

      var arrayBarcode = [123456789, 234567891, 345678912];
      var arrayPath = ['/assets/image1.jpg', '/assets/image2.jpg','/assets/image3.jpg'];
      
      var output='<ul class="slides">';
      for(var i in arrayBarcode)
      {
          output+='<li ean="'+arrayBarcode[i]+'">\
              <img src="'+arrayPath[i]+'" alt="pix" draggable="false">\
              <a data-statsaction="coupon-basket-remove" data-statscategory="coupon-buttons"\ title="Remove" href="#">\
                  <i aria-hidden="true" class="icon icon-close-neg">\
                      <span class="sr-only">Remove</span>\
                  </i>\
              </a>\
           </li>';
      }
      output+='</ul>';
      $('.flexslider').empty().append(output);
      

      DEMO

      【讨论】:

      • 不需要 2 个循环,因为 if 数组总是同步的,它们都有相同的索引,更不用说你的代码总是返回最后一个嵌套for() 循环的元素,因为您在嵌套循环之后 附加到输出! (仍然会返回错误的值)
      • 忘了提及..我最初也没有 ul.slides
      • 所以这行永远不会被执行:$('.slides').empty().append(output);
      • 干得好,斯里达尔!!完美运行。谢谢
      • @Wasiim 很高兴为您提供帮助:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2014-01-30
      • 1970-01-01
      相关资源
      最近更新 更多