How to Get Element By Class in JavaScript?

前端 未结 11 2296
南旧
南旧 2020-11-22 01:48

I want to replace the contents within a html element so I\'m using the following function for that:

function ReplaceContentInContainer(id,content) {
   var c         


        
11条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 02:36

    Of course, all modern browsers now support the following simpler way:

    var elements = document.getElementsByClassName('someClass');
    

    but be warned it doesn't work with IE8 or before. See http://caniuse.com/getelementsbyclassname

    Also, not all browsers will return a pure NodeList like they're supposed to.

    You're probably still better off using your favorite cross-browser library.

提交回复
热议问题