2013-01-20 136 views
0

這裏是我旋轉的圖像列表;如何在JavaScript中使用超鏈接?

如何單獨鏈接這些圖像中的每一個?

在一個理想的意義上,我想用這樣的連接的每一個下面的圖片中,一個HTML鏈接我會低於使用:

<a href="/banners/chiropractor/">banners/chiropractor.jpg></a> 

這裏是我完整的腳本

<html> 
<head> 

<script type="text/javascript"> 
var interval = 2.5; // delay between rotating images (in seconds) 
var random_display = 1; // 0 = no, 1 = yes 
interval *= 1000; 

var image_index = 0; 
image_list = new Array(); 
image_list[image_index++] = new imageItem("banners/chiropractor.jpg"); 
image_list[image_index++] = new imageItem("banners/chiropody.jpg"); 
image_list[image_index++] = new imageItem("banners/fitness ball.jpg"); 
image_list[image_index++] = new imageItem("banners/Dietician.jpg"); 
image_list[image_index++] = new imageItem("banners/Alexander technique.jpg"); 
var number_of_image = image_list.length; 
function imageItem(image_location) { 
this.image_item = new Image(); 
this.image_item.src = image_location; 
} 
function get_ImageItemLocation(imageObj) { 
return(imageObj.image_item.src) 
} 
function generate(x, y) { 
var range = y - x + 1; 
return Math.floor(Math.random() * range) + x; 
} 
function getNextImage() { 
if (random_display) { 
    image_index = generate(0, number_of_image-1); 
} else { 
    image_index = (image_index+1) % number_of_image; 
} 
var new_image = get_ImageItemLocation(image_list[image_index]); 
return(new_image); 
} 
function rotateImage(place) { 
var new_image = getNextImage(); 
document[place].src = new_image; 
var recur_call = "rotateImage('"+place+"')"; 
setTimeout(recur_call, interval); 
} 
</script> 

</head> 

<div> 
<img name="rImage" src="banners/chiropractor.jpg" width=222 height=218> 
<img name="rImage2" src="banners/chiropractor.jpg" width=222 height=218> 
<img name="rImage3" src="banners/chiropractor.jpg" width=222 height=218> 
<img name="rImage4" src="banners/chiropractor.jpg" width=222 height=218> 
</div> 

<script> 
rotateImage('rImage'); 
rotateImage('rImage2'); 
rotateImage('rImage3'); 
rotateImage('rImage4'); 
</script> 

</body> 
</html> 
+0

你到底想做什麼?你想添加一個鏈接到每個圖像,或者你想改變每個圖像的來源? –

+0

我想在JavaScript中包含我的每個圖像的超鏈接,而不是div標籤內的圖像。 –

+0

由於圖像從JavaScript加載,我只想超鏈接這些圖像。 –

回答

0

你的問題並不完全清楚,但如果你想要把你的圖像變成鏈接,最簡單的方法是附加一個onClick事件處理程序,像這樣...

var foo = new imageItem("banners/chiropractor.jpg"); 
//do whatever else you're doing with it... 
foo.onclick = function(){ 
    self.location.href = "http://someplace.com"; 
    //or if you want to link to the image itself 
    self.location.href = this.src; //in this case "self" is the window and "this" is the image object. 
}