We often use nodetype==1 to determine whether an element is a hmtlelement element. The elements on the page are nodes, element nodes, attribute nodes, text nodes, and so on, and so on. The definition of the NodeType of the consortium is as follows
Const unsigned short element_node = 1;
Const unsigned short attribute_node = 2;
Const unsigned short text_node = 3;
Const unsigned short cdata_section_node = 4;
Const unsigned short entity_reference_node = 5;
Const unsigned Short entity_node = 6;
Const unsigned Short processing_instruction_node = 7;
Const unsigned short comment_node = 8;
Const unsigned Short document_node = 9;
Const unsigned short document_type_node = 10;
Const unsigned short document_fragment_node = 11;
Const unsigned short notation_node = 12;
But what if our custom object also contains the NodeType attribute? Such as
Copy Code code as follows:
var obj = {Nodetype:1};
function Ishtmlelement (obj) {
if (Obj.nodetype) {
return obj.nodetype==1;
}
}
Ishtmlelement (obj);//true
The above ishtmlelement (obj) returns true, but obj is obviously not an HTML node element. The following are judged by object characteristics and Try-catch statements.
Copy Code code as follows:
function Ishtmlelement (obj) {
var d = document.createelement ("div");
try{
D.appendchild (Obj.clonenode (true));
return obj.nodetype==1?true:false;
}catch (e) {
return false;
}
}
var obj1 = {Nodetype:1};
var obj2 = document.createTextNode ("Hello");
var obj2 = document.createelement ("P");
Ishtmlelement (obj1);//false
Ishtmlelement (OBJ2);//false
Ishtmlelement (OBJ3);//true
The window and document should be handled in particular
Copy Code code as follows:
function Ishtmlcontrol (obj) {
var d = document.createelement ("div");
try{
D.appendchild (Obj.clonenode (true));
Return obj.nodetype==1? True:false;
}catch (e) {
return Obj==window | | Obj==document;
}
}