Javascript 检查元素是否为 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6518802/
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
Check if element is a div
提问by James
How do I check if $(this)
is a div
, ul
or blockquote
?
我如何检查$(this)
是div
,ul
还是blockquote
?
For example:
例如:
if ($(this) is a div) {
alert('its a div!');
} else {
alert('its not a div! some other stuff');
}
回答by Bjorn
Something like this:
像这样的东西:
if(this.tagName == 'DIV') {
alert("It's a div!");
} else {
alert("It's not a div! [some other stuff]");
}
回答by MBO
Solutions without jQuery are already posted, so I'll post solution using jQuery
没有jQuery的解决方案已经发布,所以我将使用jQuery发布解决方案
$(this).is("div,ul,blockquote")
回答by Mic
Without jQuery you can say this.tagName === 'DIV'
没有 jQuery 你可以说 this.tagName === 'DIV'
Keep in mind that the 'N' in tagName
is uppercase.
请记住,'N'tagName
是大写的。
Or, with more tags:
或者,使用更多标签:
/DIV|UL|BLOCKQUOTE/.test(this.tagName)
/DIV|UL|BLOCKQUOTE/.test(this.tagName)
回答by Andreq Frenkel
To check if this element is DIV
检查此元素是否为 DIV
if (this instanceof HTMLDivElement) {
alert('this is a div');
}
Same for HTMLUListElement
for UL,HTMLQuoteElement
for blockquote
HTMLUListElement
UL相同,HTMLQuoteElement
blockquote
回答by Dirk McQuickly
if(this.tagName.toLowerCase() == "div"){
//it's a div
} else {
//it's not a div
}
edit: while I was writing, a lot of answers were given, sorry for doublure
编辑:在我写的时候,给出了很多答案,对不起,双重错误
回答by Rup
Going through jQuery you can use $(this).is('div')
:
通过jQuery,您可以使用$(this).is('div')
:
Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.
根据选择器、元素或 jQuery 对象检查当前匹配的一组元素,如果这些元素中至少有一个与给定的参数匹配,则返回 true。
回答by brymck
Some of these solutions are going a bit overboard. All you need is tagName
from regular old JavaScript. You don't really get any benefit from re-wrapping the whole thing in jQuery again, and especially running some of the more powerful functions in the library to check the tag name. If you want to test it on this page, here's an example.
其中一些解决方案有点过火了。您所需要的只是tagName
来自常规的旧 JavaScript。再次将整个内容重新包装在 jQuery 中并没有真正获得任何好处,尤其是在库中运行一些更强大的函数来检查标记名称。如果你想在这个页面上测试它,这里有一个例子。
$("body > *").each(function() {
if (this.tagName === "DIV") {
alert("Yeah, this is a div");
} else {
alert("Bummer, this isn't");
}
});
回答by GullerYA
I'm enhancing the answer of Andreq Frenkel, just wanted to add some and it became too lengthy so gone here...
我正在增强 Andreq Frenkel 的答案,只是想添加一些,但它变得太长了,所以在这里消失了......
Thinking about CustomElements extending the existing ones and still being able to check if an element is, say, input
, makes me think that instanceof
is the best solution for this problem.
考虑 CustomElements 扩展现有元素并且仍然能够检查元素是否是,例如input
,让我认为这instanceof
是解决此问题的最佳方法。
One should be aware though, that instanceof
uses referential equality, so HTMLDivElement
of a parent window will not be the same as the one of its iframe
(or shadow DOM's etc).
但是应该注意,这instanceof
使用了引用相等性,因此HTMLDivElement
父窗口的父窗口将与其iframe
(或影子 DOM 等)不同。
To handle that case, one should use checked element's own window's classes, something like:
要处理这种情况,应该使用已检查元素自己的窗口类,例如:
element instanceof element.ownerDocument.defaultView.HTMLDivElement
element instanceof element.ownerDocument.defaultView.HTMLDivElement
回答by Ir Calif
let myElement =document.getElementById("myElementId");
if(myElement.tagName =="DIV"){
alert("is a div");
}else{
alert("is not a div");
}
/*What ever you may need to know the type write it in capitalised letters "OPTIO" ,"PARAGRAPH", "SPAN" AND whatever */