我有图片,必须计算每个图片的facebook共享。我不能为它做循环。它现在只针对一个元素。如何做到这一点?我的代码是:
<script>
$(document).ready(function(){
var current = 0;
$count = $('#count_bombs').val();
$('#' + current).each(function() {
var id = $('#' + current).val(); console.log(id);
$.getJSON('https://graph.facebook.com/http://example.com/images/view/' + id, function(data) {
document.getElementById("shares_count_" +current).innerHTML = data.shares;
console.log(data.shares); console.log("#shares_count_" +current);
});
current++;
});
});
</script> <?php $num=0; $id = 0; ?>
<?php foreach($images as $key=>$val){ ?>
<div>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo URL::base();?>images/view/<?php echo $val['image_name'];?>">
<span aria-hidden="true" class="glyphicon glyphicon-link"></span>
<input type="hidden" id="<?php echo $id; ?>" class="count" value="<?php echo $val['image_name'];?>">
SHARE
</a>
</div>
<div>
<span class="glyphicon glyphicon-comment" aria-hidden="true" ></span>
<span id="shares_count_<?php echo $id; ?>"></span>
<input type="hidden" value="<?php echo count($bombs);?>" id="count_bombs" name="count_bombs">
</div>
编辑:我做到了。我使用了每个方法的index参数。这是我的代码:
<script>
$(document).ready(function(){
var current = 0;
$("input[type=hidden]").each(function(index, element){
var id = $('#' + current).val();
$.getJSON('https://graph.facebook.com/http://pbombd.dev/bombs/view/' + id, function(data) {
$('#shares_count_' + index).html(data.shares);
});
current++;
});
});
</script>
发布于 2015-09-30 20:28:45
$('#' + count)将只返回一个元素,因此不能在其上循环。看起来你想要找到页面中每一个隐藏的带有css类count的输入。你可以这样做:
$('input.count').each(function() {
$input = $(this);
// $input contains the hidden input
console.log($input.attr('id));
});https://stackoverflow.com/questions/32866029
复制相似问题