如何在滑块图像上添加链接
How to add link on slider images
我为图像滑块使用了以下代码。以及我如何设置每个图像的链接
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var imageID=0;
function changeimage(every_seconds){
//change the image
if(!imageID){
document.getElementById("myimage").src="1.png";
imageID++;
}
else{if(imageID==1){
document.getElementById("myimage").src="2.png";
imageID++;
}else{if(imageID==2){
document.getElementById("myimage").src="3.png";
imageID=0;
}}}
//call same function again for x of seconds
setTimeout("changeimage("+every_seconds+")",((every_seconds)*1000));
}
</script>
</head>
<body style='background:black;' onload='changeimage(2)'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='center'><img width='700px' height='100px' id='myimage' src='http://www.photos.a-vsp.com/fotodb/14_green_cones.jpg'/></div>
</body>
</html>
请告诉如何添加链接到每个图像
您可以喜欢您的图像src。
使用.setAttribute('href', "yoururl");
设置带有JS的html属性。
在图像周围添加链接并更改href属性。
<script type='text/javascript'>
var imageID=0;
function changeimage(every_seconds){
var link = document.getElementById("mylink");
//change the image
if(!imageID){
document.getElementById("myimage").src="1.png";
link.setAttribute('href', "url1");
imageID++;
}
else{if(imageID==1){
document.getElementById("myimage").src="2.png";
link.setAttribute('href', "url2");
imageID++;
}else{if(imageID==2){
document.getElementById("myimage").src="3.png";
link.setAttribute('href', "url3");
imageID=0;
}}}
//call same function again for x of seconds
setTimeout("changeimage("+every_seconds+")",((every_seconds)*1000));
}
</script>
</head>
<body style='background:black;' onload='changeimage(2)'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='center'>
<a id="mylink" href="#">
<img width='700px' height='100px' id='myimage' src='http://www.photos.a-vsp.com/fotodb/14_green_cones.jpg'/>
</a>
</div>
</body>
</html>
您还可以使用if-else-if而不是嵌套的if-else语句。这是有效的。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type='text/javascript'>
var imageID=0;
function changeimage(every_seconds){
//change the image
if(!imageID){
document.getElementById("myimage").src="https://pixabay.com/static/uploads/photo/2015/08/14/08/29/images-888133_960_720.jpg";
document.getElementById("link").href = "url1";
imageID++;
}
else if(imageID==1){
document.getElementById("myimage").src="http://eyespired.nl/wp-content/uploads/2013/09/bronzen-beelden-matteo-pugliese-607x458.jpg";
document.getElementById("link").href = "url2";
imageID++;
}else if(imageID==2){
document.getElementById("myimage").src="http://www.gettyimages.ca/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg";
document.getElementById("link").href = "url3";
imageID=0;
}
//call same function again for x of seconds
setTimeout("changeimage("+every_seconds+")",((every_seconds)*1000));
}
</script>
<body style='background:black;' onload='changeimage(2)'>
<div style='position:absolute;width:100%;height:100%;left:0px;top:0px;' align='center'>
<a id="link" href="#">
<img width='700px' height='100px' id='myimage' src='http://www.photos.a-vsp.com/fotodb/14_green_cones.jpg'/>
</a></div>
</body>
相关文章:
- 如何在Angular中的表的所有单元格中添加链接
- 如何使用 HTML/JavaScript 为对象添加链接
- 如何在滑块图像上添加链接
- HTML::向输入标记添加链接
- 正在向库图像添加链接
- 在旋转javascript横幅中添加链接
- 向每个堆叠条形图项目添加链接
- 根据课程自定义字段在课程页面上添加链接
- 如何使用java脚本添加链接
- 如何在D3元素中添加链接
- 如果满足条件,则使用JQuery向元素添加链接
- 当屏幕尺寸较小时,可使用Javascript向移动菜单添加链接.不再有效
- javascript下拉列表-无法添加链接
- Ext JS Gannt 在任务之间添加链接
- 添加链接时 JQuery 停止工作
- Javascript 在添加表行时添加链接文本
- 如何在 d3.js 行添加链接名称
- Umbraco 将从第 @nodename 页添加链接作为表单上的文本输入值
- 在jQuery插件中添加链接的方法
- 在可折叠内容标头(拆分链接)中添加链接 - 缺少 css