我创建了一些X数量的图像,我希望图像的总数量显示在顶部。请帮助我如何使用Java Script或jQuery执行此操作。
<div class="images">
<div class="slide">
<img class="photo" src="img001.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img002.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img003.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img003.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img002.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img001.jpg">
<p>image description</p>
<button>Delete</button>
</div>
</div>
答案 0 :(得分:0)
$('.images img').length
应该在.images类中获取所有img标记
答案 1 :(得分:0)
使用getElementsByTagName
查找所有图片,并使用.length
计算。
var count = document.getElementsByTagName('img').length;
console.log(count);
&#13;
<div class="images">
<div class="slide">
<img class="photo" src="img001.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img002.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img003.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img003.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img002.jpg">
<p>image description</p>
<button>Delete</button>
</div>
<div class="slide">
<img src="img001.jpg">
<p>image description</p>
<button>Delete</button>
</div>
</div>
&#13;