最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Cross browser Selecting elements by class name - Stack Overflow

matteradmin4PV0评论

How do I select all elements using same class name in javascript. I know i can do it using document.getElementsByClassName but I read somewhere that it's not cross browser so if it is true what is the appropriate way to select elements depending on class name without jQuery or other library.

Thanks!

How do I select all elements using same class name in javascript. I know i can do it using document.getElementsByClassName but I read somewhere that it's not cross browser so if it is true what is the appropriate way to select elements depending on class name without jQuery or other library.

Thanks!

Share Improve this question edited Apr 29, 2012 at 21:10 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Apr 29, 2012 at 20:43 user1349047user1349047 2
  • 2 Believe me when I say you are not the first person to ask this question... google.co.uk/search?q=select+by+class+name+javascript – Matt Commented Apr 29, 2012 at 20:45
  • 1 @Raj give querySelectorAll() a try, it's better and more flexible than getElement...() – Christoph Commented Apr 29, 2012 at 21:01
Add a ment  | 

3 Answers 3

Reset to default 8

I found this code:

if (!document.getElementsByClassName) {
    document.getElementsByClassName = function(classname) {
        var elArray = [];
        var tmp = document.getElementsByTagName("*");
        var regex = new RegExp("(^|\\s)" + classname + "(\\s|$)");
        for (var i = 0; i < tmp.length; i++) {

            if (regex.test(tmp[i].className)) {
                elArray.push(tmp[i]);
            }
        }

        return elArray;
    };
}​

Here

see here:

Support for getElementsByClassName

I remend using querySelector. It's more natural and pretty close to the jQuery Syntax, thus more mon to most ppl. Also it's pretty fast and you don't need to distinguish between classes, ids or whatever.

If you want to Support IE<7, you need a shim like gdoron provided.

It may be better to use document.querySelector or document.querySelectorAll, supported since IE8.

Have a look here:

https://developer.mozilla/docs/Web/API/document.querySelector https://developer.mozilla/docs/Web/API/document.querySelectorAll

Post a comment

comment list (0)

  1. No comments so far