Home >Web Front-end >JS Tutorial >JavaScript cross-browser method to obtain the same class node in the page_javascript skills
When developing web pages, many times we need to operate elements with the same class name, that is, elements with the same class. I took the written test yesterday and I couldn’t answer a related question:
JavaScript gets the node with class test in the page
So I collected some relevant information and listed two methods that I think are better in this article. If there are any shortcomings, I hope everyone can criticize and correct them. If you have a better method, I hope you can share it.
Solution1 Jeremy Keuth Solution
Uncle Jeremy Keuth talked about the getElementsByClass method in Chapter 3, Section 4 of the book "The Art of JavaScript DOM Programming (2nd Edition)" (English: DOM Scripting-Web Design with JavaScript and the Document Object Model). It also talks about how to apply this method in browsers that do not support this attribute (IE6, IE7 and IE8, let us despise them). The excerpt is here, with modifications in some places.
There is a new method in HTML5 DOM that allows us to access elements through the class name in the class attribute. This is: getELementsByClassName. Since the method is relatively new, it is not yet available in some DOM implementations, so be careful when using it. . Let's first take a look at what this method can do for us, and then discuss how to use this method reliably.
Similar to the getELementsByTagName method, getElementsByClassName only accepts one parameter, which is the class name: