如何在javascript中获取当前图像的数量

时间:2012-04-03 08:36:31

标签: php javascript drupal

我在我的网站上使用Drupal 7和Views模块。并希望在我当前的缩略图图像上显示编号。搜索StackoverFlow并找到此代码:

HTML CODE:
<img src="http://www.ambuvet.com/images/white-cat.jpg" />
<img src="http://www.biobest.co.uk/assets/images/diagnostics/cats.jpg" />
<img src="http://animal.discovery.com/guides/atoz/gallery/cat.jpg" />

JAVASCRIPT CODE:
window.onload = function() {
for (var i = 0; i < document.images.length; i++) {
    var image = document.images[i];
    image.setAttribute("index", i + "");
    image.onclick = function() {
        var index = this.getAttribute("index");
        alert("This is image #" + index);
    };
}
};

请参阅此链接:http://jsfiddle.net/uUF8x/

但我不知道,我如何在当前缩略图上替换此代码获取数字。感谢。

1 个答案:

答案 0 :(得分:0)

使用缩略图创建div作为背景图像,如

HTML

<div class="thumbnail" style="background-image: url(http://www.ambuvet.com/images/white-cat.jpg);width:100px;height:100px;"></div>
<div class="thumbnail" style="background-image: url(http://www.biobest.co.uk/assets/images/diagnostics/cats.jpg);width:100px;height:100px;"></div>
<div class="thumbnail" style="background-image: url(http://animal.discovery.com/guides/atoz/gallery/cat.jpg);width:100px;height:100px;"></div>

JS

window.onload = function() {
    $(".thumbnail").each(function(index){
    $(this).html(index+1); // Will insert number inside div tag, use css to position it where you need
})
};​