Jquery 计数图像或;rel";属性
我想分别计算一张Jquery 计数图像或;rel";属性,jquery,image,count,each,rel,Jquery,Image,Count,Each,Rel,我想分别计算一张的所有图像 我的代码: 包含2*个图像 我试试这个 var-featureImageCount=$('div.thumbnail img').length; $('div.缩略图')。在(''+featureImageCount+'Images')之后; 但我得到的只是所有的div”(6) 我可以用jquery这样做吗?图像的数量*将自动填充? 或者jquery可以通过不同的rel属性来统计项目吗 谢谢 Ogni您需要使用.each单独查看每个.thumbnail元素。然
的所有图像
我的代码:
包含2*个图像
我试试这个
var-featureImageCount=$('div.thumbnail img').length;
$('div.缩略图')。在(''+featureImageCount+'Images')之后;
但我得到的只是所有的div”(6)我可以用jquery这样做吗?图像的数量*将自动填充?
或者jquery可以通过不同的
rel
属性来统计项目吗
谢谢Ogni您需要使用
.each
单独查看每个.thumbnail
元素。然后,您可以在回调中使用$(this)
来访问实际的元素。从那里,您可以找到所有特定的子代
元素。从那里,您可以像您尝试的那样使用.after,使用$(this)
$(文档).ready(函数(){
$(“div.缩略图”)。每个(函数(){
变量$this=$(this),
count=$this.children(“a”).children(“img”).length;
$this.after(“+count+”图像”);
});
});
$('div.缩略图')。每个(函数(){
变量$this=$(this),
count=$this.find('img').length;
$this.after(“”{
文本:'包含('+count+'*)图像'+(count==1?'''s')
});
});
谢谢。我更新了你的小提琴,因为我只得到了“1”:[link]@ogni你可能看到了一个奇怪的版本。如果你看看我现在的答案中的小提琴,它似乎对我有用-显示“3”,“2”,“1”谢谢。我也更新了我的版本,因为我的代码结构不正确,一些图像是display:none
,我使用一个fancybox库,只有一个链接的预览图像。谢谢-我完成了$('.thumbnail')。每个(函数(){var$this=$(this),count=$this.find('a').length;$this.after(“+count+“images”););代码>
<div class="thumbnail">
<a rel="first" href="#">
<div>
<div></div>
<img src="...">
</div>
</a>
<div class="hidden-fb"> <!-- // CSS display: none -->
<a rel="first" href="#">
<div>
<div></div>
<img src="...">
</div>
</a>
</div><!-- .hidden-fb -->
</div><!-- .thumbnail -->
<h2>Contains 2* images </h2>
var featureImageCount = $('div.thumbnail img').length;
$('div.thumbnail').after('<div><h1>' + featureImageCount + ' Images</h1></div>');
$(document).ready(function () {
$("div.thumbnail").each(function () {
var $this = $(this),
count = $this.children("a").children("img").length;
$this.after("<div><h1>" + count + " Images</h1></div>");
});
});
$('div.thumbnail').each(function () {
var $this = $(this),
count = $this.find('img').length;
$this.after('<h2>', {
text: 'Contains (' + count + '*) image' + (count == 1 ? '' : 's')
});
});