Why getElementsByClassName do not work in Mozilla?

泪湿孤枕 提交于 2020-01-30 12:21:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!