问题
I am trying to get an element of a webpage by using getElementsByClassName. I typed in firebug console
getElementsByClassName('classname');
and it returns with
ReferenceError: getElementsByClassName is not defined
I read in this page that firefox supports getElementsByClassName. I have updated firefox. Why I am getting this error?
回答1:
You need to use the correct format. It is a method of the document
object.
document.getElementsByClassName('classname');
回答2:
Its like this :
document.getElementsByClassName('yourClassName')
回答3:
If it is a method of window
object then you can directly call those methods like:
console.log()
But if it is a method of any other object you have to call the method like object.methodName()
Since getElementsByClassName
is a method of document
object you have to call it as
document.getElementsByClassName('classname')
回答4:
try something like this
document.getElementsByClassName('test');
rootElement.getElementsByClassName('names');
回答5:
Simple as that
<div class="myclass"></div>
<div clsss="myclass"></div>
var classElementArray=document.getElementsByClassName('myclass')
回答6:
JS:
window.onload=function() {
var holder = document.getElementsByClassName('holder');
for (var i=0; i<holder.length; i++) {
holder[i].innerHTML='<span>' + holder[i].className + '</span> ';
}
}
HTML:
<div class="box">
<div class="holder">
</div>
</div>
You may forward by link:
http://jsfiddle.net/QP3Ky/
来源:https://stackoverflow.com/questions/20089569/why-getelementsbyclassname-do-not-work-in-mozilla