Не все браузеры изначально поддерживают getElementsByClassName
, хотя ситуация улучшается. Вы можете использовать функцию, которая проверяет нативную реализацию и использует ее, если она найдена, или же захватывает все элементы и проверяет каждый из них на предмет имени класса, возвращая массив совпадающих элементов.
function getElementsByClassName( className, context ) {
//the context is the container we will confine our search to (optional)
context = context || document;
//use native implimentation if it exists
if( context.getElementsByClassName ) {
return context.getElementsByClassName( className ); //returns a nodeList
}
//we have to do it ourselves if we get here
var candidates = context.getElementsByTagName( '*' );
var found = [];
//regular expression to match the classname as per comments
var rxp = new RegExp( "(?:^|\\s)" + className + "(?:\\s|$)");
for( var i = 0, l = candidates.length; i < l; i++ ) {
if( rxp.test( className ) {
found.push( candidates[i] );
}
}
return found; //returns an array of nodes
}