简体   繁体   中英

How to check class name using javascript??

How to check class name using javascript?? I am using this statement.. if (document.getElementByClass("expandable")

but it give me error.. "Object doesn't support this property or method"

Because you just made that method up!

You want document.getElementsByClassName: https://developer.mozilla.org/en/DOM/document.getElementsByClassName

There is no function document.getElementByClass . Either check for the property .className of potential objects, or use a higher level framework which implements this (Such as jquery ). This is the syntax with jquery:

$(".expandable")

Via pure javascript:
You can use the getElementsByClassName from this question

With jQuery, this is lot more simple, use the class selector :

$(".className)
  1. There is no function like getElementByClass .

  2. You have to use document.getElementsByClassName ; it will return an array so you have to use loop to get values.

But MSIE will not support this function so you have to check your class name like:

((document.getElementsByTagName("div"))[i].className)=="yourclassname"

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