Testing the type of a DOM element in JavaScript

前端 未结 7 1050
孤街浪徒
孤街浪徒 2020-12-13 16:46

Is there a way to test the type of an element in JavaScript?

The answer may or may not require the prototype library, however the following setup does make use of t

相关标签:
7条回答
  • 2020-12-13 17:29

    Perhaps you'll have to check the nodetype too:

    if(element.nodeType == 1){//element of type html-object/tag
      if(element.tagName=="a"){
        //this is an a-element
      }
      if(element.tagName=="div"){
        //this is a div-element
      }
    }
    

    Edit: Corrected the nodeType-value

    0 讨论(0)
提交回复
热议问题