首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何遍历所有元素jQuery

问如何遍历所有元素jQuery
EN

Stack Overflow用户
提问于 2015-09-30 20:24:14
回答 1查看 92关注 0票数 0

我有图片,必须计算每个图片的facebook共享。我不能为它做循环。它现在只针对一个元素。如何做到这一点?我的代码是:

​

代码语言:javascript
复制
        <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>
代码语言:javascript
复制
        <?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参数。这是我的代码:

​

代码语言:javascript
复制
<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>

​

EN

回答 1

Stack Overflow用户

发布于 2015-09-30 20:28:45

$('#' + count)将只返回一个元素,因此不能在其上循环。看起来你想要找到页面中每一个隐藏的带有css类count的输入。你可以这样做:

代码语言:javascript
复制
$('input.count').each(function() {
    $input = $(this);

    // $input contains the hidden input
    console.log($input.attr('id));
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32866029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档