小码哥的IT人生

XML DOM text 属性

XML基础 2023-08-12 18:00:20小码哥的IT人生shichen

XML DOM text 属性

定义和用法

text 属性返回属性的文本。

语法:

attrObject.text

提示和注释

注释:此属性是 Internet Explorer 专用的。

实例

在所有的例子中,我们将使用 XML 文件 books.xml,以及 JavaScript 函数 loadXMLDoc()

下面的代码片段显示 XML 文档中 category 属性的文本:

xmlDoc=loadXMLDoc("/example/xdom/books.xml");
x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++)
{
document.write(x.item(i).attributes[0].text);
document.write("<br />");
}

以上代码的输出:

children
cooking
web
web

TIY

完整实例【显示属性的文本】:

<html>
<head>
<script type="text/javascript" src="/demo/example/xdom/loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("/demo/example/xdom/books.xml");
x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++)
{
document.write(x.item(i).attributes[0].text);
document.write("<br />");
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024