2011-04-15 148 views
3

我使用了“为什么这个幻灯片闪烁?”代码。哪些彼得已经给你的答案在网站上,它的工作完全正常,没有任何闪烁,但我的问题是如何添加超链接到每个图像。 我已经粘贴代码,供大家参考如何添加链接到幻灯片中的每个图像?

脚本

(function() {  
    // ------  
    // ###### Edit these.  
    // Assumes you have images in path named 1.jpg, 2.jpg etc.  
    var imagePath = "images";  
    var lastImage = 5;   
    // How many images do you have?  
    var fadeTime = 4000;  
// var index=1; 
    // Time between image fadeouts.  
    // ------  
    // ###### Don't edit beyond this point.  
    // No need for outer index var  
    function slideShow(index) {     
    var url = imagePath + "/" + index + ".jpg";     
    // Add new image behind current image   
    $("#slideShow").prepend($("<img/>").attr("src",url));  
    // Fade the current image, then in the call back   
    // remove the image and call the next image   
    $("#slideShow img:last").fadeOut("slow", function() {    
    $(this).remove(); 
    setTimeout(function() {     
    slideShow((index % lastImage) + 1)    
    }, fadeTime);   

    });  
    }  
    $(document).ready(function() {   
    // Img 1 is already showing, so we call 2  
    setTimeout(function() { slideShow(2)}, fadeTime);  
    delay(1000); 
    }); })(); 

</script> 

请帮助.................

回答

0

在此之后:

$("#slideShow").prepend($("<img/>").attr("src",url)); 

尝试添加该

$('#slideshow img').wrap('<a href="location" />'); 

您将需要改变位置到您想要的链接去的

0

您的线路在这里:

$("#slideShow").prepend($("<img/>").attr("src",url)); 

(我真的不知道JQuery的或曾经这是什么,我却理解JavaScript + HTML)

您可以加入一个:

.attr("onClick",JAVASCRIPTNAMEHERE) 

要结束了吗?

$("#slideShow").prepend($("<img/>").attr("src",url).attr("onClick","window.Navigate('www.Google.com')"); 
+0

DalexL谢谢回答我,你有上面给出的代码将所有的图像链接到同一“www.google.com”,但我需要不同的链接为每个图像。 – 2011-04-15 17:08:34

+0

你知道这种语言有多少?难道你不能使用某种简单的if语句或从文件中读取数据来确定链接吗?我的例子只是例子。为什么把每个人都发送到谷歌:P(这是一个很棒的网站,虽然:3) – FreeSnow 2011-04-15 19:56:44

+0

我尝试插入你之前说过的线路并没有解决问题。该图像没有显示任何链接。 – 2011-04-15 20:26:49

0

代替

$("#slideShow").prepend($("<img/>").attr("src",url)); 

尝试使用

$("#slideShow").prepend($("<a href='" + url + "'><img src='" + url + "' /></a>")); 

HTH