How do I get jQuery Fancybox to accept my object as a valid open parameter?

£可爱£侵袭症+ 提交于 2019-12-25 03:56:18

问题


I have some html in this format:

<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>

I want to step through it and provide it to Fancybox like this.

I've got as far as this, but it's straight-up not working:

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 gives me:

{"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 the downloadArray object gives me:

{"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>"}} 

Any help would be appreciated.


回答1:


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);



回答2:


My extended answer based on @Adrian's answer :

In order to download (show in fancybox) the proper logo according to the link selected, we use the API option index like in the following tweaked extended code :

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

See JSFIDDLE



来源:https://stackoverflow.com/questions/14323922/how-do-i-get-jquery-fancybox-to-accept-my-object-as-a-valid-open-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!