Fancybox:数组变量未传递 [英] Fancybox: Array variable not being passed

查看:79
本文介绍了Fancybox:数组变量未传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎不可能发生这种情况!

Just can't seem to get this happening !

$(".fancy").click(function(event) {
  event.preventDefault();
  var pic = [];
  $('.room-thumbnail').each(function(index) {
      pic.push('\'' + 'http://localhost' + $(this).attr('href') + '\'' );
  });

  var pics = '['+ pic.join(', ')+']';

  console.log(pics);
  // => ['http://localhost/driver/images/produits/denim/sixthjune-7440/sixthjune-7440.jpg', 'http://localhost/driver/images/produits/denim/sixthjune-7440/sixthjune-7440-back.jpg', 'http://localhost/driver/images/produits/denim/sixthjune-7440/sixthjune-7440-closeup1.jpg', 'http://localhost/driver/images/produits/denim/sixthjune-7440/sixthjune-7440-closeup2.jpg']

  $.fancybox(pics, {
          'transitionIn'      : 'elastic',
          'transitionOut'     : 'elastic',
          'overlayColor'      : '#1D1D1D',
          'type'              : 'image',
          'cyclic': true
      });
});

如果我手动输入href网址(pics),它将起作用. 但是,当我通过fancybox传递变量(pics)时,我收到了fancybox错误: -无法加载请求的内容. - 请稍后再试. -

If I enter the href urls ( pics ) manually it works. But when I pass fancybox the variable ( pics ) i get the fancybox error : - The requested content cannot be loaded. - Please try again later. -

有人可以告诉我我做错了什么吗?

Can someone tell me what I doing wrong ?

谢谢.

推荐答案

您的代码没有错,但是您没有考虑某些问题:

There is nothing wrong with your code but there is something that you are not considering:

您的声明var pics = '['+ pic.join(', ')+']';返回一个fancybox无法解析的 string .

Your declaration var pics = '['+ pic.join(', ')+']'; is returning a string that fancybox cannot parse.

您需要做的是将此类 string 转换为JavaScript object,以便fancybox可以对其进行解析.有不同的方法可以做到这一点.一种是使用eval()函数,但是可能存在安全问题,因此不建议使用此方法.

What you have to do is to convert such string into a javascript object so fancybox can parse it. There are different ways to do it. One is using the eval() function, but there can be security issues so this method is not recommended.

由于您使用的是jQuery,最安全的方法是使用 jQuery.parseJSON(json)

Since you are using jQuery, your safest way to do it is to use jQuery.parseJSON( json )

...所以就在设置变量pics

... so just right after the line where you set your variable pics

var pics = '['+ pic.join(', ')+']';

添加此代码可将其转换为js对象

add this to convert it to a js object

pics = jQuery.parseJSON(pics);

应该可以解决问题.

这篇关于Fancybox:数组变量未传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆