Do we have getElementsByClassName in javascript?

后端 未结 5 586
生来不讨喜
生来不讨喜 2020-11-27 08:09

Just like in jQuery we can use $(\".classname\"), is there a similar thing in javascript as well? Or if we don\'t have such method then how can i implement that.
Accordi

5条回答
  •  醉酒成梦
    2020-11-27 08:17

    Yes, you'd have to iterate to support all browsers. If you do, tho, make sure you take advantage of the browser's built in functionality where it exists:

    if(document.getElementsByClassName) {
       return document.getElementsByClassName(className);
    } else {
       // iterate
    }
    

    Other than that, I'm with Jochen; use a framework

提交回复
热议问题