e4x:如何最好地检查属性是否存在?

发布于 2024-08-04 21:08:41 字数 639 浏览 10 评论 0原文

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

>x = <a attr1='33' />
>x.@attr1
33
>[email protected]()
1
>[email protected]()
0

所以我可以使用

if ([email protected]() > 0)
{
    .... do something ....
}

,但是有更合适的方法吗?

I know I can do it with the length() method:

>x = <a attr1='33' />
>x.@attr1
33
>[email protected]()
1
>[email protected]()
0

so I could use

if ([email protected]() > 0)
{
    .... do something ....
}

but is there a more appropriate way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-08-11 21:08:42

没关系,我通过仔细研究Ecma-357 标准<找到了答案/a>,特别是 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

Never mind, I found the answer by poring through the Ecma-357 standard, particularly the XML.prototype.* and XMLList.prototype.* sections 13.4 and 13.5.

It's the hasOwnProperty() method:

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
怀中猫帐中妖 2024-08-11 21:08:42

最简单的方法:

(@attr1 in theXML)

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

easiest way:

(@attr1 in theXML)

this will return true if id attrtibute exists and false otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文