网络编程 
首页 > 网络编程 > 浏览文章

判断目标是否是window,document,和拥有tagName的Element的代码

(编辑:jimmy 日期: 2025/9/22 浏览:3 次 )
复制代码 代码如下:
function isWindow( obj )
{
if( typeof obj.closed == 'undefined' ) return false;
var result = /\[object (window|global)\]/i.test( Object.prototype.toString.call( obj ) );
if( result )return result;
try{
obj.closed = obj.closed;
return false;
}catch(e)
{
result = true;
}
return result;
}
function isDocument( obj )
{
if( typeof obj.body == 'undefined' ) return false;
var b = obj.body;
try{
obj.body = null;
obj.body = b;
return false;
}catch(e)
{
return true;
}
}
function isElement( o )
{
var tn = 'tagName',temp = o[tn],result;
if( typeof temp == 'undefined' )return false;
try{
o[tn] = null;
result = ( temp == o[tn] );
o[tn] = temp;
return result;
}catch(e)
{
return true;
}
}

function getOwnerWindow( node )
{
if( isWindow( node ) )return node;
var doc = isDocument( node ) ? node : node.ownerDocument;
return doc.view || doc.parentWindiw || doc.defaultView;
}

需要充分测试
上一篇:js 表格排序(编辑+拖拽+缩放)
下一篇:js 数组的for循环到底应该怎么写?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap