无法tagname到div??

无法tagname到div??

http://img1.sycdn.imooc.com/climg//597be4e10001e53518840776.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 封装一个代替getElementById()的方法
function byId(id){
    return typeof(id)==="string"?document.getElementById(id):id;
}
 
// 全局变量
var index = 0,
    timer = null,
    pics = byId("banner").getElementsByTagName("div"),
    len = pics.length;
   
function slideImg(){
    var main = byId("main");
    // 滑过清除定时器,离开继续
    main.onmouseover = function(){
        // 滑过清除定时器
    }
    main.onmouseout = function(){
        timer = setInterval(function(){
            index++;   // len=3 0 1 2
            if(index>=len){
                index = 0;
            }
 
        },3000);
    }
 
}
 
slideImg();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>banner轮播</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="main" id="main">
<!-- 图片轮播 -->
<div class="banner" id="banner">
<a href="">
<div class="banner-slide slide1 slide-active"></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="button prev"></a>
<a href="javascript:viod(0)" class="button next"></a>
<div class="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  *{
      margin: 0;
      padding:0;
  }
 ul{
   list-style: none;
 }
 
 body{
   font-family: "微软雅黑";
   color: #14191e;
 }
 
 .main{
   width: 1200px;
   height: 460px;
   margin: 30px auto;
   overflow: hidden;
   position: relative;
 }
 
 .banner{
   width: 1200px;
   height: 460px;
   position: relative;
   overflow: hidden;
 }
 .banner-slide{
   width: 1200px;
   height: 460px;
   position: absolute;
   background-repeat: no-repeat;
   display: none;
 }
 
 .slide-active{
   display: block;
 }
 
 .slide1{
   background-image:url(../img/bg1.jpg);
 
 }
 
  .slide2{
   background-image:url(../img/bg2.jpg);
 }
  .slide3{
   background-image:url(../img/bg3.jpg);
 }
 
.button{
    position: absolute;
    width: 40px;
    height: 80px;
    left:244px;
    top: 50%;
    margin-top: -40px;
    background: url(../img/arrow.png) no-repeat center center;
}
 
.button:hover{
    background-color: #333;
    opacity: 0.2;
    /*兼容其他浏览器*/
    filter: alpha(opacity=20);
}
.next{
    left: auto;
    right: 0;
}
 
.prev{
    transform: rotate(180deg);
}
 
.dots{
    position: absolute;
    right:20px;
    bottom: 24px;
    cursor: pointer;
}
 
.dots span{
    display: inline-block;
    width: 12px;
    height: 12px;
    background: rgba(7,17,27,0.4);
    border-radius: 50%;
    margin-left: 8px;
    border: 2px solid #fff;
 
}
 
.dots span.active{
    border: 2px solid rgba(7,17,27,0.4) ;
    background: #fff;
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
怎么都被占用了呢 2017-07-29 16:49:08

你确定你自己运行的代码是你放上来的这些吗(完整吗)

怎么都被占用了呢 2017-07-29 14:57:33

你好,经过测试了你上传的这些代码,没有出现你的这种报错,而且你代码中的这种操作pics = byId("banner").getElementsByTagName("div"),是没有问题的,可以获取到3个div元素的。

  • 提问者 OlafChou #1
    啊,那咋办啊。。。。
    2017-07-29 15:52:22
  • 老师,我的也是这个问题,一直报错。非要把byId("banner")替换成document才不报错。这到底是咋回事?
    2017-10-06 00:11:10
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码