2012-04-03 59 views
0

我在我的網站中使用Drupal 7和Views模塊。並希望顯示我當前的縮略圖圖片上的編號。搜索計算器,找到這個代碼:如何在javascript中獲取當前圖像的編號

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/

但我不知道,我怎麼能在當前縮略圖上取代對GET號此代碼。謝謝。

+1

總是交相關代碼**在問題本身**,不只是鏈接到它。爲什麼:http://meta.stackexchange.com/questions/118392/add-stack-overfow-faq-entry-or-similar-forputing-code-in-the-question – 2012-04-03 08:39:55

+0

人們如何幫助你,如果你不要說你的「想要」? – tusar 2012-04-03 08:44:14

+0

編輯我的帖子,對不起。 – Karmacoma 2012-04-03 08:45:49

回答

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 
}) 
};​ 
+0

感謝Sandeep Manne,你可以在jsfiddle上看到這段代碼嗎?非常感謝。 – Karmacoma 2012-04-03 09:23:05

+0

檢查此http://jsfiddle.net/uUF8x/13/ – 2012-04-03 09:34:47

+0

許多很多謝謝!這很棒! – Karmacoma 2012-04-03 09:38:03

相關問題