jQuery 使用jQuery查找元素的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/608410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:26:52  来源:igfitidea点击:

finding the type of an element using jQuery

jquery

提问by jyotishka bora

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?

在 jQuery 中,如果我有一个元素的引用,我如何确定它是什么类型的元素,例如,输入或下拉列表?有什么办法可以查到吗?

Duplicate:

复制:

How can I determine the element type of a matched element in jQuery?

如何确定jQuery中匹配元素的元素类型?

回答by Marius

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

如果元素是输入,则以下将返回 true:

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

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

或者您可以使用以下内容来获取标签的名称:

$("#elementId").get(0).tagName

回答by Resolution

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

您可以使用.prop()tagName为你想要得到的属性的名称:

$("#elementId").prop('tagName'); 

回答by Matas Vaitkevicius

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

值得注意的是,@Marius 的第二个答案可以用作纯 Javascript 解决方案。

document.getElementById('elementId').tagName