0

我有一些这种格式的html:

<div id="logos" class="downloadlist">
    <ul>
        <li><a href="/toolkit/logos/download.jpg" data-image="images/toolkit/logos/one.jpg">First Logo</a></li>
        <li><a href="/toolkit/logos/download.jpg" data-image="images/toolkit/logos/two.jpg">Second Logo</a></li>
        <li><a href="/toolkit/logos/download.jpg" data-image="images/toolkit/logos/three.jpg">Third Logo</a></li>
    </ul>
</div>

我想逐步完成它并像这样将它提供给 Fancybox 。

我已经做到了这一点,但它直接不起作用:

downloadList = $('#logos ul li a');

var downloadArray = {};

downloadJSON = '';

$.each(downloadList, function(d)
{
    var download = $(this);
    var href = download.data('image');
    var title = download[0].innerText;
    var link = download[0].href;

    downloadArray[d] = {};
    downloadArray[d].href = href;
    downloadArray[d].title = "<a href=\'" + link + "\'>"+title+"</a>";

    downloadJSON += JSON.stringify(downloadArray[d])+',';
});

downloadJSON = downloadJSON.slice(0, -1);

$.fancybox.open([downloadArray]);

downloadJSON 给了我:

{"href":"images/toolkit/logos/one.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>First Logo</a>"},{"href":"images/toolkit/logos/two.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>Second Logo</a>"},{"href":"images/toolkit/logos/three.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>Third Logo</a>"}

JSON.stringify-ing downloadArray 对象给了我:

{"0":{"href":"images/toolkit/logos/one.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>First Logo</a>"},"1":{"href":"images/toolkit/logos/two.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>Second Logo</a>"},"2":{"href":"images/toolkit/logos/three.jpg","title":"<a href='http://example.com/toolkit/logos/download.jpg'>Third Logo</a>"}} 

任何帮助,将不胜感激。

4

2 回答 2

2
links = [];
$('#logos ul li a').each(function() {
   links.push({
      href: $(this).attr('data-image'),
      title: "<a href=\'" + $(this).attr('href') + "\'>"+$(this).html()+"</a>"
   });
});
//JSON.stringify(links);
$.fancybox.open(links);
于 2013-01-14T18:08:33.760 回答
1

我的扩展答案基于@Adrian 的回答:

为了根据所选链接下载(显示在花式框中)正确的徽标,我们使用 API 选项index,如下面的调整扩展代码:

links = [];
$('#logos ul li a').each(function (indx) {
  // Adrain's code tweaked
  links.push({
    href: $(this).data('image'),
    title: "<a href=\'" + $(this).data('image') + "\'>" + $(this).html() + "</a>"
  });
  // extended to catch click on links
  $(this).on("click", function (e) {
    e.preventDefault();
    $.fancybox.open(links, {
      index: indx // starts gallery with index of the clicked link
    });
  });
});
//JSON.stringify(links);
//$.fancybox.open(links); // would open fancybox on page load

JSFIDDLE

于 2013-01-15T00:18:31.250 回答