How to get the name of an element with Javascript?

后端 未结 5 593
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 01:16

I\'m trying to get the name of an element in Javascript. Meaning if the element is

, then \"div\" would be returned. If it\'s &l
相关标签:
5条回答
  • 2021-01-20 01:23

    tagName or nodeName

    0 讨论(0)
  • 2021-01-20 01:23

    You want element.nodeName Or, within jQuery:

    $(".includeMe").each(function(){
      alert(this.nodeName);
    });
    
    <img class="includeMe" src="puppies.jpg" />
    <div class="includeMe">Hello World</div>
    <p   class="includeMe">Don't forget me as well</p>
    
    0 讨论(0)
  • 2021-01-20 01:28
    $(selector).each(function() {
        switch (this.tagName) {
            // Handle cases
        }
    });
    
    0 讨论(0)
  • 2021-01-20 01:43

    Use nodeName (see this note about tagName):

    "My advice is not to use tagName at all. nodeName contains all functionalities of tagName, plus a few more. Therefore nodeName is always the better choice."

    0 讨论(0)
  • 2021-01-20 01:46

    For element.tagName and element.nodeName return the name of your tag, in uppercase.

    If you want it in lowercase, just use element.tagName.toLowerCase() or element.nodeName.toLowerCase().

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