为什么点击右边的切换图片按钮没效果,报错了
来源:2-3 轮播图上按钮样式
大圣归来²
2020-07-15 13:18:22
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="tets.css">
<script type="text/javascript" src="tets.js" defer="true"></script>
</head>
<body>
<div class="main" id="main" >
<div class="banner" id="banner">
<a href="">
<div class="banner-slide slide1 slide-activ">
</div>
</a>
<a href="">
<div class="banner-slide slide2 ">
</div>
</a>
<a href="">
<div class="banner-slide slide3" >
</div>
</a>
</div>
<a href="javascript:viod(0)" class="botton prev" id="pre"></a>
<a href="javascript:viod(0)" class="botton next" id="next"></a>
</a>
<div class="dots" id="dots">
<span class="active"></span>
<span class=""></span>
<span class=""></span>
</div>
</div>
</body>
</html>
function byId(id){
return typeof(id)==="string"?document.getElementById(id):id;
}
var index = 0,
timer = null,
pics=byId("banner").getElementsByTagName("div"),
len=pics.length,
pre=byId(pre),
next=byId(next),
dots=byId("dots").getElementsByTagName("span");
function slideImg(){
var main=byId("main");
main.onmouseover=function(){
if (timer) clearInterval(timer);
}
main.onmouseout=function(){
timer = setInterval(function(){
index++;
if(index >=len){
index=0;
}
changeImg();
console.log(index);
},4000);
}
main.onmouseout();
for(var d=0;d<len;d++){
dots[d].id=d;
dots[d].onclick=function(){
index=this.id;
this.className="active"
changeImg();
}
}
next.onclick = function(){
index++;
if(index>=len) index=0;
changeImg();
}
}
function changeImg(){
for(var i=0;i<len;i++)
{
pics[i].style.display="none";
dots[i].className="";
}
pics[index].style.display='block';
dots[index].className="active";
}
slideImg();
*{
padding: 0;
margin: 0;
}
ul{
list-style: none;
}
body{
font-size: "微软雅黑";
color:#14191e;
}
.main{
width: 1200px;
height: 460px;
margin: 0 auto;
text-align: center;
position: relative;
}
.banner{
width: 1200px;
height: 460px;
}
.banner-slide{
width: 1200px;
height: 460px;
background: no-repeat;
position: absolute;
display: none;
}
.slide-activ{
display: block;
}
.slide1{
background-image: url("img/bg1.jpg");
}
.slide2{
background-image: url("img/bg2.jpg");
}
.slide3{
background-image: url("img/bg3.jpg");
}
.main .prev{
width: 40px;
height: 80px;
top: 40%;
position: absolute;
left: 240px;
background: url("img/arrow.png") no-repeat center;
transform: rotate(180deg);
}
.main .next{
width: 40px;
height: 80px;
top: 40%;
background: url("img/arrow.png") no-repeat center;
position: absolute;
left: auto;
right: 0;
}
.main .prev:hover{
background-color: black;
opacity: 0.6;
}
.main .next:hover{
background-color: black;
opacity: 0.6;
}
.dots{
position: absolute;
right: 10px;
bottom: 20px;
text-align: right;
}
.dots span{
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: white;
line-height: 12px;
margin-left: 8px;
background: rgb(7,17,27,0.4);
box-shadow: 0 0 0 2px rgba(255,255,255,0.5) inset;
}
.dots span.active{
box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;
background: #fff;
}
/*
.main .banner .banner-slide2{
width: 1200px;
height: 460px;
background:no-repeat;
background-image: url("img/bg2.jpg");
}
.main .banner .banner-slide3{
width: 1200px;
height: 460px;
background:no-repeat;
background-image: url("img/bg3.jpg");
}
*/
1回答
好帮手慕阿慧
2020-07-15
同学你好,byId传入的值错误,导致获得不到右边按钮元素,无法绑定点击事件。传入的id值应该是使用引号引起来。参考代码如下:

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题