The getElementsByClassName method has been added in DOM3, but other versions other than IE9 and 10 are not supported, which is a pain!
At present, this can be solved. It is to determine whether the browser supports this method. If it is supported, it doesn't matter; if it is not supported, add the getElementsByClassName method to the document object. This writing method has an advantage, that is, you don't need to modify the code regardless of whether there are native functions or not.
Some people on the Internet directly define a getElementsByClassName function, but in this way, you need to rewrite all the document.getElementsByClassName in the code to getElementsByClassName. It's a bit inconvenient and not universal.
The following method perfectly supports document writing:
if(!document.getElementsByClassName){ document.getElementsByClassName = function(className, element){ var children = (element || document).getElementsByTagName('*'); var elements = new Array(); for (var i=0; i<children.length; i++){ var child = children[i]; var classNames = child.className.split(' '); for (var j=0; j<classNames.length; j++){ if (classNames[j] == className){ elements.push(child); break; } } } return elements; };}