0

我知道我可以用 length() 方法做到这一点:

>x = <a attr1='33' />
>x.@attr1
33
>x.@attr1.length()
1
>x.@attr2.length()
0

所以我可以使用

if (x.@someattr.length() > 0)
{
    .... do something ....
}

但是有更合适的方法吗?

4

2 回答 2

1

没关系,我通过仔细研究Ecma-357 标准找到了答案,特别是 XML.prototype.* 和 XMLList.prototype.* 第 13.4 和 13.5 节。

方法是hasOwnProperty()

js>x = <a attr1='33' ><item>gumball!</item></a>
<a attr1="33">
  <item>gumball!</item>
</a>
js>x.@attr1
33
js>x.hasOwnProperty('@attr1');
true
js>x.hasOwnProperty('@attr2');
false
js>x.hasOwnProperty('item');
true
js>x.hasOwnProperty('mongoose');
false
于 2009-09-17T18:25:37.383 回答
0

最简单的方法:

(@attr1 in theXML)

如果 id 属性存在,这将返回 true,否则返回 false。

于 2013-02-26T08:42:07.277 回答