最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check class name using javascript?? - Stack Overflow

programmeradmin4浏览0评论

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"

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"

Share Improve this question asked Aug 5, 2011 at 8:56 SSTSST 4595 gold badges20 silver badges35 bronze badges 3
  • 1 doubled question stackoverflow./questions/1422629/… – Anatoly Commented Aug 5, 2011 at 8:59
  • if you want to check class name of some element - document.getElementById('myid').className. What you are trying to do is get elements that have some class as it seems to me... – Gatekeeper Commented Aug 5, 2011 at 9:04
  • possible duplicate of Get All Elements in an HTML document with a specific CSS Class – Felix Kling Commented Aug 5, 2011 at 10:43
Add a ment  | 

4 Answers 4

Reset to default 2

Because you just made that method up!

You want document.getElementsByClassName: https://developer.mozilla/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"
发布评论

评论列表(0)

  1. 暂无评论