为什么提示Cannot read property 'getElementsByClassName' of undefined

新手上路,请多包涵

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/text.css">

</head>
<body>

<!--c单栏-->
<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>
    <!--圆点切换图片-->
    <div class="dots" id="dots">
        <span class="active"></span>
        <span ></span>
        <span ></span>
    </div>
    <!--上一张,下一张按钮-->
    <a href="javascript:void(0)" class="button prev" id="prev"></a>
    <a href="javascript:void(0)" class="button next" id="next"></a>
    <script src="js/text.js"></script>
<!--导航菜单-->
    <div class="menu-box">
    <!--主菜单-->
    <div class="menu-content">
        <!--内容-->
        <div class="menu-item">
            <a href="#">
            <span>手机、配件</span>
            <i>&#xe665;</i></a>
        </div>
        <div class="menu-item">
            <a href="#">
            <span>电脑</span>
            <i>&#xe665;</i></a>
        </div>
        <div class="menu-item">
            <a href="#">
            <span>家用电器</span>
            <i>&#xe665;</i></a>
        </div>
        <div class="menu-item">
            <a href="#">
            <span>家具</span>
            <i>&#xe665;</i></a>
        </div>
    </div>
    <!--内容-->
    <div class="sub-menu hide">
        <!--手机配件-->
        <div class="inner-box">
            <!--手机配件-->
            <div class="sub-inner-box">
                <div class="title">手机、配件</div>
                <div class="sub-row">
                    <span class="bold mr10">手机通讯:</span>
                    <a href="">手机</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">手机维修</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">以旧换新</a>
                </div>
                <div class="sub-row">
                    <span class="bold mr10">手机配件:</span>
                    <a href="">手机壳</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">手机存储卡</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">数据线</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">充电器</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">电池</a>
                </div>
                <div class="sub-row">
                    <span class="bold mr10">运营商:</span>
                    <a href="">中国联通</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">中国移动</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">中国电信</a>
                </div>
                <div class="sub-row">
                    <span class="bold mr10">智能设备:</span>
                    <a href="">智能手环</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">智能家居</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">智能手表</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">其他配件</a>
                </div>
                <div class="sub-row">
                    <span class="bold mr10">娱乐:</span>
                    <a href="">耳机</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">音响</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">收音机</a>
                    <span class="ml10 mr10">/</span>
                    <a href="">麦克风</a>
                </div>
            </div>        
        </div>
        <!--电脑-->
        <div class="inner-box">
            <div class="sub-inner-box">
                <div class="title">电脑</div>
                <div class="sub-row">
                   <span class="bold mr10">电脑:</span>
                   <a href="">笔记本</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">平板</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">一体机</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">电脑配件:</span>
                   <a href="">显示器</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">CPU</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">主板</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">硬盘</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">电源</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">显卡</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">其他配件</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">游戏设备:</span>
                   <a href="">游戏机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">耳机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">游戏软件</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">网络产品:</span>
                   <a href="">路由器</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">网络机顶盒</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">交换机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">存储卡</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">网卡</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">外部产品:</span>
                   <a href="">鼠标</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">键盘</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">U盘</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">移动硬盘</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">鼠标垫</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">电脑清洁</a>
                </div>
            </div>
        </div>
            <!--家用电器-->
        <div class="inner-box">
            <div class="sub-inner-box">
                <div class="title">家用电器</div>
                <div class="sub-row">
                   <span class="bold mr10">电视:</span>
                   <a href="">国产品牌</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">韩国品牌</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">欧美品牌</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">空调:</span>
                   <a href="">显示器</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">柜式</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">中央</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">壁挂式</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">冰箱:</span>
                   <a href="">多门</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">对开门</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">三门</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">双门</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">洗衣机:</span>
                   <a href="">滚筒式洗衣机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">迷你洗衣机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">洗烘一体机</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">厨房电器:</span>
                   <a href="">油烟机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">洗碗机</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">燃气灶</a>
                </div>
            </div>
        </div>
            <!--家具-->
        <div class="inner-box">
            <div class="sub-inner-box">
                <div class="title">家具</div>
                <div class="sub-row">
                   <span class="bold mr10">家纺:</span>
                   <a href="">被子</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">枕头</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">四件套</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">床垫</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">灯具:</span>
                   <a href="">台灯</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">顶灯</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">节能灯</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">应急灯</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">厨具:</span>
                   <a href="">烹饪锅具</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">餐具</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">菜板刀具</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">家装:</span>
                   <a href="">地毯</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">沙发垫套</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">装饰字画</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">照片墙</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">窗帘</a>
                </div>
                <div class="sub-row">
                   <span class="bold mr10">生活日用:</span>
                   <a href="">收纳用品</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">浴室用品</a>
                   <span class="ml10 mr10">/</span>
                   <a href="">雨伞雨衣</a>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
</div>

</body>
</html>

var index = 0,

timer = null,
banner= byId("banner").getElementsByTagName("div"),
dots  = byId("dots").getElementsByTagName("span"),
len   = banner.length,
prev  = byId("prev"),
next  = byId("next"),
menu  = byId("menu-content"),
subMenu   = byId("sub-menu"),
innerBox  = subMenu.getElementsByClassName("inner-box"),
menuItems = menu.getElementsByClassName("menu-item");

阅读 7.9k
4 个回答

你menu-content是通过id获取到的,在dom中没有id为menu-content的,好像还有其它的几个你也获取错了

是否还没有domReady?

新手上路,请多包涵

byId是什么

愉快的告诉你,submenun没有获取到,是undefined

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题