如何知道如果对象是一个HTML文档使用的JavaScript [英] how to know if an object is an Html document using javascript
问题描述
我传递一个对象说, OBJ
,给一个函数。 OBJ
可能可能的的任何类型的 - (TemplatedHelper,AlertMessage,PartialViews,HTMLDocument的,等等)的我想知道,如果 OBJ
是一个HTML文档。什么是可能的方式来实现呢?
I'm passing an object say, obj
, to a function. obj
could possibly of any type - (TemplatedHelper, AlertMessage, PartialViews, HTMLDocument, etc.) I want to know if obj
is an HTML Document. What are the possible ways to achieve it?
我已经尝试使用
var containerCount = $(obj).length;
for (var ctr = 0; ctr < containerCount; ctr++) {
var containerTagName = $(obj)[ctr].tagName;
alert(containerTagName); // to know all detected tagNames
// this returns LINK, SCRIPT, DIV, INPUT, etc..
if ((containerTagName == "TITLE") || (containerTagName == "HTML")) {
var isHTML = true;
break;
}
}
在preceding code,铬只检测标题
标记,但IE8不检测 HTML
,头
和标题
标记。虽然这些片段codeS不IE8工作太:
with the preceding code, Chrome only detects the title
tag, but IE8 doesn't detect html
, head
, and title
tags. While these fragment codes don't work in IE8 too:
alert($(obj).has('title')); // or 'html' as element parameter, returns [object Object]
alert($(obj).find('title')); // or 'html' as element parameter, returns [object Object]
if ($(obj)[ctr].parent())
alert($(obj)[ctr].parent().get(0).tagName); // returns undefined
请我分享你的想法吧。在此先感谢!
Please share me your thoughts about it. Thanks in advance!
推荐答案
试试这个:
if (obj instanceof HTMLDocument)
{
// obj is a HTMLDocument
}
if (Object.prototype.toString.call(obj) == "[object HTMLDocument]")
{
// obj is a HTMLDocument
}
这篇关于如何知道如果对象是一个HTML文档使用的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!