2016-06-09 145 views
-4

我有3张图片但不在线,我需要将这三张图片链接到不同的页面上! 每幅图像各个环节Javascript link in image

var images = [ 
 
    'image/dragao1.jpg', 
 
    'image/dragao2.jpg', 
 
    'image/dragao3.jpg' 
 
]; 
 

 
function slideShowForward() { 
 
    images.push(images.shift()); 
 
    document.getElementById('imagesP').style.backgroundImage = 'url(' + images[0] + ')'; 
 
} 
 

 
function slideShowBack() { 
 
    images.unshift(images.pop()); 
 
    document.getElementById('imagesP').style.backgroundImage = 'url(' + images[0] + ')'; 
 
}
<div id="imagesP" style=" border: 1px solid black; height: 200px; width:750px;"> 
 
    <div id="form"> 
 
    <input type="button" value="previous" onclick="slideShowBack();" style="width: 39px; height: 34px;" /> 
 
    <input type="button" value="next" onclick="slideShowForward();" style="width: 39px; height: 34px;" /> 
 
    </div> 
 
</div>

+0

请编辑您的问题,编辑代码段并将您的HTML _there_,**放入评论**中。仍然没有提及任何地方的链接。你是什​​么意思_“我必须把图像链接”_? – Xufox

+0

你需要添加一个onclick到div如果你想点击图片去某个地方 – mplungjan

回答

0

var images = [ 
 
    'http://cinemacomrapadura.com.br/imagens/2014/01/20140127-como-treinar-seu-dragao1.jpg', 
 
    'http://cinemacomrapadura.com.br/imagens/2014/01/20140127-como-treinar-seu-dragao2.jpg', 
 
    'http://br.web.img2.acsta.net/c_520_690/newsv7/15/02/06/17/22/496087.jpg' 
 
]; 
 
var hrefs=["http://www.google.com","http://www.msdn.com","http://www.yahoo.com"] 
 

 
function slideShowForward() { 
 
    images.push(images.shift()); 
 
    hrefs.push(hrefs.shift()); 
 
    document.getElementById('imagesP').style.backgroundImage = 'url(' + images[0] + ')'; 
 
    document.getElementById('imagesP').setAttribute(href,hrefs[0]); 
 
} 
 

 
function slideShowBack() { 
 
    images.unshift(images.pop()); 
 
    hrefs.unshift(hrefs.pop()); 
 
    document.getElementById('imagesP').style.backgroundImage = 'url(' + images[0] + ')'; 
 
    document.getElementById('imagesP').setAttribute(href,hrefs[0]); 
 
} 
 
window.onload=function() { 
 
    document.getElementById('imagesP').onclick=function(e) { 
 
    var tgt = e.target||e.srcElement; 
 
    if (tgt.tagName.toLowerCase()=="input") e.preventDefault(); 
 
    else location=this.getAttribute("href"); 
 
    } 
 
}
#imagesP { border: 1px solid black; height: 200px; width:750px; background-image: url(http://br.web.img2.acsta.net/c_520_690/newsv7/15/02/06/17/22/496087.jpg)}
<div id="imagesP" href="http://www.google.com"> 
 
    <div id="form"> 
 
    <input type="button" value="previous" onclick="slideShowBack();" style="width: 39px; height: 34px;" /> 
 
    <input type="button" value="next" onclick="slideShowForward();" style="width: 39px; height: 34px;" /> 
 
    </div> 
 
</div>

+0

谢谢,但只有一个链接正在运行谷歌 –

+0

由于安全原因,Google和其他人可能无法在代码段中执行此操作 - 但是我也使用了奇怪的数组处理功能,以免发生太多变化。应该工作 – mplungjan