2009-11-06 89 views
0

我试图创建一个淡入淡出效果与以下...我被告知即时消息几乎那里除了传递的JSON数组。此刻没有图像显示。如何正确传递JSON数组?

//generate all the boxes 
$.get('images.php',function(data){ 
    for (var i=0; i < totalBoxes; i++){ 
     var randomImage = data[Math.floor(Math.random() * data.length)]; 
     $('<div class="pf-box"><img class="black" src="' + randomImage['black'] + '" /><img class="colour" src="' + randomImage['colour'] + '" /></div>').hide().appendTo('#bg').fadeIn('slow').filter('.colour').css("opacity", 0); 
    } 
},'json'); 

//add the hover behavior to all the elements 
$('.colour').hover(function() { 
    $(this).stop().fadeTo(700, 1); 
},function() { 
    $(this).stop().fadeTo(700, 0); 
}); 

和images.php

<?php 
    header('Content-type: application/json'); 
echo '[ 
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'}, 
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'} 
]'; 
    ?> 

回答

0

使用randomImage.black代替randomImage [“黑”]

+0

有在JavaScript – 2009-11-06 01:00:52

+0

两者之间没有差异存在于JavaScript的关联数组没有官方的支持..给那些点回 – 2009-11-06 01:02:26

+0

我不知道我已经删除对不起 – Andy 2009-11-06 01:03:43

2

你不需要逃避JSON字符串内的报价?否则,PHP解释器将无法发送所有你想要的东西,甚至可能会漏掉一些错误。

+0

我想创建这样的效果: http://www.yellostudio.co.uk/temp/index.php 上面的代码上 http://www.yellostudio.co.uk/temp /indexV2.php 你知道我要去哪里吗? – Andy 2009-11-06 01:00:16

0

您的回音是因为JSON输出爆发回声的单引号的失败。

附上不同的报价你的绳子,让你能够正确回应:

<?php 
header('Content-type: application/json'); 
echo "[ 
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'}, 
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'} 
]"; 
?> 

注意使用双引号括而不是你使用单引号回声字符串。 (如果你在字符串中有双引号,那么你可以将其翻转并在外部使用单引号)。