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

Categories

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

jquery - Javascript: Multiple GetElementByID's in one call

Really simple question. I have the following code, and would like to consolidate it down as much as possible. Can't get it to work though.. tried using commas etc. Is there a way to call more than one ID in the GetElementByID method? New to Javascript : /

$("#set50").live("click", function() {
    document.getElementById("statusBox50").setAttribute("class", "statusBoxSelected");
    document.getElementById("statusBox25").setAttribute("class", "statusBox");
    document.getElementById("statusBox75").setAttribute("class", "statusBox");
    document.getElementById("statusBox100").setAttribute("class", "statusBox");
    $(".statusbar").animate({
        width: '115px'
    }, 500);    
});
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

No, but since you're using jQuery anyway, you can use that. This will add the statusBox class to the elements with the IDs statusBox25, statusBox75, and statusBox100:

$("#statusBox25, #statusBox75, #statusBox100").addClass('statusBox');

Alternatively, if you want to remove all of the existing classes and replace them all with statusBox like your original code as doing, you could use this:

$("#statusBox25, #statusBox75, #statusBox100").attr('class', 'statusBox');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...