如何使用Javascript获取元素的名称? [英] How to get the name of an element with Javascript?
问题描述
我想在Javascript中获取元素的名称。意思是如果元素是< div />
,那么将返回div
。如果它是< img src =/>
则将返回img
。我使用jQuery来选择一些元素,然后在所有元素上调用一个自定义函数。在这个功能中,我想知道我正在处理什么。我该怎么做?
I'm trying to get the name of an element in Javascript. Meaning if the element is <div />
, then "div"
would be returned. If it's <img src="" />
then "img"
would be returned. I'm using jquery to select a bunch of elements and then calling a custom function on all of them. Within that function I want to know what I'm dealing with. How do I do this?
似乎是一件简单的事情。而且我以为我以前做过,但是我找不到它。 Google结果不断给我get element by name,不管我怎么说这个词。
Seems like a simple thing. And I think I've done it before but I just can't find it. Google results keep giving me "get element by name" no matter how I phrase it.
推荐答案
使用 nodeName (见关于tagName的注释):
我的建议是不要使用tagName所有
nodeName包含tagName的所有功能,再加上一些,因此nodeName总是更好的选择。
"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."
这篇关于如何使用Javascript获取元素的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!