javascript - document对象的方法无法通过元素来调用

查看:87
本文介绍了javascript - document对象的方法无法通过元素来调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

HTML代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>万年历</title>
    <link rel="stylesheet" type="text/css" href="css/rl.css">
    <script type="text/javascript">
        window.onload=function(){
            var oContainer=document.getElementsByClassName('container');

            // 触发器
            var aBtn=document.getElementsByTagName('button');


            // 被操纵对象
            var oCont=oContainer.getElementsByClassName('container');

            var arr=['你好啊我们一起去意大利',
            '哈哈哈哈哈哈哈哈哈哈哈哈',
            'llllllllllllllllllll',
            'inaongd',
            '555555555555555555555555555',
            '666666666666',
            '7777777777777777777',
            '88888888888',
            '99999999999',
            '101010',
            '111111111111111111111',
            '1212121212',
            ];

            for (var i = aBtn.length - 1; i >= 0; i--) {
                aBtn[i].onmouseover=function() {
                    aBtn[i].index=i;
                    oCont.innerHTML='<h3>'+(this.index+1)+'月活动</h3><p>'+arr[this.index]+'</p>';
                }
            }
        }
    
    </script>
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <div class="row">
                <div><button>1</button></div>
                <div><button>2</button></div>
                <div><button>3</button></div>
            </div>
            <div class="row">
                <div><button>4</button></div>
                <div><button>5</button></div>
                <div><button>6</button></div>
            </div>
            <div class="row">
                <div><button>7</button></div>
                <div><button>8</button></div>
                <div><button>9</button></div>
            </div>
            <div class="row">
                <div><button>10</button></div>
                <div><button>11</button></div>
                <div><button>12</button></div>
            </div>
        </div>
        <div class="content">
            <h3>1月活动</h3>
            <p>要去拜年事业有成</p>
        </div>
    </div>
</body>
</html>

CSS代码:

.container{
    width: 190px;
    margin: 0 auto;
    position: relative;
}
.wrapper{
    display: table;
    border-collapse: separate;
    border-spacing: 10px 10px;
    background: #ccc;
    margin:0 auto;
}
.row{
    display: table-row;
}
.row div,
.row button{
    display: table-cell;
    height:50px;
    width: 50px;
}
.row button{
    font-size: 25px;
    background: #333;
    color: #fff;
    border:none;
}
.row button:hover{
    cursor: pointer;
    background: #23aa11;
}
.content{
    position: absolute;
    left: 0;
    top: 250px;
    width: 190px;
    display: block;
    background: #ccc;
}
.content h3,
.content p{
    margin: 10px;
}
.content h3{
    font:bold 16px/1 '黑体';
}
.content p{
    font-size: 14px;
}

报错截图:

报错信息:

Uncaught TypeError: oContainer.getElementsByClassName is not a function at window.onload

解决方案

document.getElementsByClassName返回的是HTMLCollection不是DOM对象,你需要遍历。
你这里oContainer是一个DOM数组。
getElementsByClassName

这篇关于javascript - document对象的方法无法通过元素来调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆