getElementByClassName不返回结果 [英] getElementByClassName Not Returning Results

查看:660
本文介绍了getElementByClassName不返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 getElementByClassName()没有返回任何结果,我把它设置为 getElementById(),但是我不能使用 Id ,因为同一个函数需要应用于七个链接。我已经填写了有关 jsFiddle 的所有信息

My getElementByClassName() isn't returning any results, I had it set to getElementById(), but I can't use the Id since the same function will need to apply to seven links. I have filled in all the information on jsFiddle

javascript看起来像:

The javascript looks like:

var myBoxWidth = 0;
var myBoxWidth2 = 0;

// show
function show() {
    var myBox = document.getElementByClassName('box');  
    var myContent = document.getElementByClassName('content');
    myContent.style.display = 'inline';
    myBox.style.width = myBoxWidth + '%';  
    if(myBoxWidth < 80) {  
        myBoxWidth += 20;
        setTimeout(show,55);
    }
}

// hide
function hide() {
    var myBox = document.getElementByClassName('box');
    var myContent = document.getElementByClassName('content');
    myContent.style.display = 'none';
    var currentWidthVal = parseInt(myBox.style.width,10);
    if(myBoxWidth2 < currentWidthVal) {  
        setTimeout(hide,55);
        myBox.style.width = currentWidthVal =  currentWidthVal - 20 + '%';
        myBoxWidth = 0;
    }
}


推荐答案

那里不是这样的 getElementByClassName()。尝试 getElementsByClassName()

there is no such getElementByClassName(). Try getElementsByClassName()

document.getElementsByClassName('..')返回一组元素而你的代码是在期望它将返回单个元素的情况下编写的。您可以将该部分更改为

document.getElementsByClassName('..') returns a set of elements while your code is written with expectation that it'll return single element. You could change that part to

var myContent = document.getElementsByClassName('content');

var num = myContent.length;

for(var x=0; x < num; x++){
    myContent[x].style.display = 'block'; //or whatever style you've in your original code 
}

这篇关于getElementByClassName不返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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