Testing the type of a DOM element in JavaScript

前端 未结 7 1058
孤街浪徒
孤街浪徒 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

提交回复
热议问题