2016-05-30 43 views
0

我正在尝试使用Backstretch来显示照片。该插件需要此​​代码与一个按钮对应才能显示照片。使用PHP循环jQuery

$("#someId").click(function(e) { 
    e.preventDefault(); 
    $.backstretch('http://dl.dropbox.com/u/515046/www/outside.jpg'); 
}); 

我有PHP构建一个动态的网站,我可以依次通过我的照片创建缩略图和链接,但我不能得到的值到这个代码。这个jQuery代码需要通过来自PHP照片循环的数据循环。 我尝试使用.each()循环来获取数据,但仍然没有运气。

这里是我迄今为止

$(".id").each(function(){ 
    var id; 
    var photo; 

    $(this).attr('id', function(i, val){ 
     id = '#' + val; 
     $(".img").each(function(){ 
      $(this).attr('src', function(i, val){ 
       photo = val; 
      }); 
     }); 
console.log(id) 
console.log(photo) 
     $(id).click(function(e) { 
      e.preventDefault(); 
      $.backstretch(photo); 
     }); 
    }); 
}); 

似乎没有什么,工作,任何帮助,将不胜感激。

HTML

<?php 
$category = "urban"; 
$photos = Photo::find_all_category($category); 
?> 

<script src="js/plugins/jquery.js"></script> 
<script src="js/plugins/jquery.backstretch.js"></script> 

<div class="row urban"> 
    <div class="col-lg-2"> 
     <div class="row"> 
<?php foreach($photos as $photo): ?> 
     <a class="id" id="<?php echo $photo->id; ?>" src="admin/images/<?php echo $photo->filename; ?>"><img class="img-responsive" src="admin/images/<?php echo $photo->filename; ?>" alt=""></a> 

<?php endforeach; ?> 
     </div> 
    </div> 
    <div class="col-lg-10"> 
     <div class="backstretch"></div> 
    </div> 
</div> 
+1

请携带您的HTML代码。看来你是每个**使用嵌套的**不正确。 –

回答

0

我改变了格式

$(".id").each(function(){ 
var id; 
var photo; 
id = '#' + (this).getAttribute('id'); 
console.log(id); 
photo = (this).getAttribute('src'); 
console.log(photo); 
$(id).click(function(e) { 
     e.preventDefault(); 
     $(".backstretch").backstretch(photo); 
    }); 
}); 

而且还发现了我,包括jQuery的两倍。删除它,它的工作。谢谢!

0

你可以简单地针对类名:

$('.id').click(function(ev){ 
    ev.preventDefault(); 
    $(".backstretch").backstretch($(this).attr('src')); 
});