简体   繁体   中英

Why getElementsByClassName do not work in Mozilla?

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?

You need to use the correct format. It is a method of the document object.

document.getElementsByClassName('classname');

就像这样 :

document.getElementsByClassName('yourClassName')

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')

try something like this

  document.getElementsByClassName('test');
  rootElement.getElementsByClassName('names');

Simple as that

<div class="myclass"></div>
<div clsss="myclass"></div>

var classElementArray=document.getElementsByClassName('myclass')

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM