isElement - 确定对象是否为元素 [英] isElement - determining if an object is an element

查看:63
本文介绍了isElement - 确定对象是否为元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于M $愚蠢而没有让DOMElements成为头等舱公民,

以下将不起作用: -


function isElement(o)

{

返回o instanceof元素

}


适用于FF,Opera和Safari 。


原型的作用是什么: -


isElement:function(object){

return object& ;&安培; object.nodeType == 1

}


我的版本使用浏览器嗅探: -


函数isElement(o )

{

if(!isIE)

return o instanceof Element

else

return o&& o.nodeType == 1&& o.tagName!=未定义

}


测试用例: -

http://www.aarongray.org/Test/JavaSc...ment-test.html


实际的''isIE''测试代码并不是最好的,但用于简洁。


任何创造,问题或改进大多数欢迎。


谢谢,


Aaron

解决方案

不能让DOMElements成为头等舱公民的愚蠢

以下将不起作用: -


功能isElement(o)

{

返回o instanceof元素

}


适用于FF,Opera和Safari。


原型的作用是什么: -


isElement:function(object){

return object&& object.nodeType == 1

}


我的版本使用浏览器嗅探: -


函数isElement(o )

{

if(!isIE)

return o instanceof Element

else

return o&& o.nodeType == 1&& o.tagName!=未定义

}


测试用例: -

http://www.aarongray.org/Test/JavaSc...ment-test.html


实际的''isIE''测试代码并不是最好的,但用于简洁。


任何创造,问题或改进大多数欢迎。


谢谢,


Aaron


7月30日,2:下午46点,Aaron Gray写道:

< snip>


函数isElement(o)

{

if(!isIE)

返回o instanceof元素

else

return o&& o.nodeType == 1&& o.tagName!=未定义

}



< snip>


为什么分支为这个?如果第二次测试足够好,那么

总是足够好。


1的精确相等(===)比类型更精确转换

平等并减少可能的误报。


但总的来说这是什么意思?如果您从

a的真实结果推断出您的 - isElement - 方法,该对象实现了整个W3C Core DOM(1级或2级)元素接口的

错了

在许多情况下。从这样的元素中决定你想要的功能是什么更有意义,并且在需要的基础上测试那些单独的功能



"亨利" < rc ******* @ raindrop.co.ukwrote in message

news:35 ********************* ************* @ 2g2000hs n.googlegroups.com ...


7月30日下午2:46,Aaron Gray写道:

< snip>


>函数isElement(o)
{
如果(!isIE)
返回o instanceof元素

返回o&& o.nodeType == 1&& o.tagName!= undefined
}



< snip>


为什么要为此分支?如果第二次测试足够好,那么

总是足够好。



因为我想使用符合浏览器的特性,并不是说这实际上是一个,但它应该在我看来。我明白你的观点。但

首次测试更快,更准确,可在更多浏览器上运行。


与1完全相等(===)比类型转换更精确

平等并减少可能的误报。



是的,忘了改变它。谢谢,


但总体而言,重点是什么?如果您从

a的真实结果推断出您的 - isElement - 方法,该对象实现了整个W3C Core DOM(1级或2级)元素接口的

错了

在许多情况下。从这样的元素决定你想要的功能是什么更有意义,并且在需要的基础上测试那些单独的那些因为



基本上我的库小部件构造函数要么采用DOM结构,

通常是封闭的DIV(这是我想要测试的)或者直接(或通过AJAX)描述对象的对象J / b $ b JSON之类的对象。


感谢您的反馈,


Aaron


Due to M$''s stupidity in not making DOMElements first class citizens the
following will not work :-

function isElement( o)
{
return o instanceof Element
}

It works for FF, Opera and Safari.

What prototype does is this :-

isElement: function(object) {
return object && object.nodeType == 1
}

My version used Browser sniffing :-

function isElement( o)
{
if (!isIE)
return o instanceof Element
else
return o && o.nodeType == 1 && o.tagName != undefined
}

Test case :-

http://www.aarongray.org/Test/JavaSc...ment-test.html

The actual ''isIE'' test code is not the best but is used for brevity.

Any crevats, problems or enhancements most welcome.

Thanks,

Aaron

解决方案

''s stupidity in not making DOMElements first class citizens the
following will not work :-

function isElement( o)
{
return o instanceof Element
}

It works for FF, Opera and Safari.

What prototype does is this :-

isElement: function(object) {
return object && object.nodeType == 1
}

My version used Browser sniffing :-

function isElement( o)
{
if (!isIE)
return o instanceof Element
else
return o && o.nodeType == 1 && o.tagName != undefined
}

Test case :-

http://www.aarongray.org/Test/JavaSc...ment-test.html

The actual ''isIE'' test code is not the best but is used for brevity.

Any crevats, problems or enhancements most welcome.

Thanks,

Aaron


On Jul 30, 2:46 pm, Aaron Gray wrote:
<snip>

function isElement( o)
{
if (!isIE)
return o instanceof Element
else
return o && o.nodeType == 1 && o.tagName != undefined
}

<snip>

Why branch for this? If the second test is ever good enough it is
always good enough.

Exact equality (===) with 1 would be more precise than type-converting
equality and reduce the possible false positives.

But overall what it the point? If you inferred from a true result from
a call to your - isElement - method that the object implemented the
whole W3C Core DOM (Level 1 or 2) Element interface you would be wrong
in a number of cases. It would make more sense to decide what features
you wanted from such an element and test for those alone on a basis
driven by need.


"Henry" <rc*******@raindrop.co.ukwrote in message
news:35**********************************@2g2000hs n.googlegroups.com...

On Jul 30, 2:46 pm, Aaron Gray wrote:
<snip>

> function isElement( o)
{
if (!isIE)
return o instanceof Element
else
return o && o.nodeType == 1 && o.tagName != undefined
}

<snip>

Why branch for this? If the second test is ever good enough it is
always good enough.

Because I would like to use "conforming" browser traits, not that this is
actually one, but it should have been in my view. I take your point. But the
first test is faster and more accurate and runs on more browsers.

Exact equality (===) with 1 would be more precise than type-converting
equality and reduce the possible false positives.

Yes forgot to change that. Thanks,

But overall what it the point? If you inferred from a true result from
a call to your - isElement - method that the object implemented the
whole W3C Core DOM (Level 1 or 2) Element interface you would be wrong
in a number of cases. It would make more sense to decide what features
you wanted from such an element and test for those alone on a basis
driven by need.

Basically my library widget constructors either take a DOM structure,
normally the enclosing DIV (which is what I was wanting to test for) or a
JSON like object which describes the object, directly (or over AJAX).

Thanks for the feedback,

Aaron


这篇关于isElement - 确定对象是否为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆