Detect element tag name by its class

前端 未结 5 514
情话喂你
情话喂你 2021-01-26 19:39

I would like to know if there is a way to get the name of a element by its class or id.

for exemple retur

相关标签:
5条回答
  • 2021-01-26 19:49

    use this:

    $('.classTag').get(0).tagName;
    

    or this:

    $('.classTag')[0].tagName;
    

    for ex we have this markup:

    <div class=classTag>my div </div>
    
    
    var $tag = $('.classTag')[0].tagName; //this will return 'DIV'as result
    alert($tag);
    
    0 讨论(0)
  • 2021-01-26 19:51

    Try tagName :

    var input = document.getElementsByClassName("some-class")[0];
    alert(input.tagName);
    

    JSFiddle: http://jsfiddle.net/PG656/1/

    0 讨论(0)
  • 2021-01-26 19:51

    Actually $('.some-class') returns an array of elements with this class, so you have to loop through it:

    $('.some-class').each(function(){ 
        console.log(this.nodeName); 
    });
    

    this will output their node names to console

    0 讨论(0)
  • 2021-01-26 20:05

    you can use:

    var input = document.getElementsByClassName("some-class")[0]
    alert(input.type);
    alert(input.name);
    
    0 讨论(0)
  • var tagName = $('.some-class').prop('tagName');
    

    the name of the tag will be returned in capital letters

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