Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...