finding the type of an element using jQuery

前端 未结 3 540
慢半拍i
慢半拍i 2020-11-27 09:38

In jQuery, if I have a reference to an element, how can I determine what kind of element it is, for example, an input or an dropdown? Is there any way to find out?

相关标签:
3条回答
  • 2020-11-27 09:44

    It is worth noting that @Marius's second answer could be used as pure Javascript solution.

    document.getElementById('elementId').tagName
    
    0 讨论(0)
  • 2020-11-27 09:59

    The following will return true if the element is an input:

    $("#elementId").is("input") 
    

    or you can use the following to get the name of the tag:

    $("#elementId").get(0).tagName
    
    0 讨论(0)
  • 2020-11-27 10:04

    You can use .prop() with tagName as the name of the property that you want to get:

    $("#elementId").prop('tagName'); 
    
    0 讨论(0)
提交回复
热议问题