Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

javascript - getElementByClassName Not Returning Results

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

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Update

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 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...