我有一些这种格式的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>"}}
任何帮助,将不胜感激。