admin管理员组文章数量:1134553
Is there a way to test the type of an element in JavaScript?
The answer may or may not require the prototype library, however the following setup does make use of the library.
function(event) {
var element = event.element();
// if the element is an anchor
...
// if the element is a td
...
}
Is there a way to test the type of an element in JavaScript?
The answer may or may not require the prototype library, however the following setup does make use of the library.
function(event) {
var element = event.element();
// if the element is an anchor
...
// if the element is a td
...
}
Share
Improve this question
edited Dec 22, 2014 at 16:23
user3189338
asked Oct 8, 2008 at 21:42
Casey WatsonCasey Watson
52.6k10 gold badges35 silver badges30 bronze badges
1
|
7 Answers
Reset to default 136You can use typeof(N)
to get the actual object type, but what you want to do is check the tag, not the type of the DOM element.
In that case, use the elem.tagName
or elem.nodeName
property.
if you want to get really creative, you can use a dictionary of tagnames and anonymous closures instead if a switch or if/else.
if (element.nodeName == "A") {
...
} else if (element.nodeName == "TD") {
...
}
Perhaps you'll have to check the nodetype too:
if(element.nodeType == 1){//element of type html-object/tag
if(element.tagName=="a"){
//this is an a-element
}
if(element.tagName=="div"){
//this is a div-element
}
}
Edit: Corrected the nodeType-value
roenving is correct BUT you need to change the test to:
if(element.nodeType == 1) { //code }
because nodeType of 3 is actually a text node and nodeType of 1 is an HTML element. See http://www.w3schools.com/Dom/dom_nodetype.asp
Although the previous answers work perfectly, I will just add another way where the elements can also be classified using the interface they have implemented.
Refer W3 Org for available interfaces
console.log(document.querySelector("#anchorelem") instanceof HTMLAnchorElement);
console.log(document.querySelector("#divelem") instanceof HTMLDivElement);
console.log(document.querySelector("#buttonelem") instanceof HTMLButtonElement);
console.log(document.querySelector("#inputelem") instanceof HTMLInputElement);
<a id="anchorelem" href="">Anchor element</a>
<div id="divelem">Div Element</div>
<button id="buttonelem">Button Element</button>
<br><input id="inputelem">
The interface check can be made in 2 ways as elem instanceof HTMLAnchorElement
or elem.constructor.name == "HTMLAnchorElement"
, both returns true
I usually get it from the toString() return value. It works in differently accessed DOM elements:
var a = document.querySelector('a');
var img = document.createElement('img');
document.body.innerHTML += '<div id="newthing"></div>';
var div = document.getElementById('newthing');
Object.prototype.toString.call(a); // "[object HTMLAnchorElement]"
Object.prototype.toString.call(img); // "[object HTMLImageElement]"
Object.prototype.toString.call(div); // "[object HTMLDivElement]"
Then the relevant piece:
Object.prototype.toString.call(...).split(' ')[1].slice(0, -1);
It works in Chrome, FF, Opera, Edge, IE9+ (in older IE it return "[object Object]").
I have another way of testing the same.
Element.prototype.typeof = "element";
var element = document.body; // any dom element
if (element && element.typeof == "element"){
return true;
// this is a dom element
}
else{
return false;
// this isn't a dom element
}
本文标签: prototypejsTesting the type of a DOM element in JavaScriptStack Overflow
版权声明:本文标题:prototypejs - Testing the type of a DOM element in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736823299a1954381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tagName
property – Soldeplata Saketos Commented Jan 4, 2022 at 7:10