Javascript selectSingleNode起作用,但selectNodes不起作用

Javascript selectSingleNode起作用,但selectNodes不起作用,javascript,internet-explorer,xmlhttprequest,selectsinglenode,selectnodes,Javascript,Internet Explorer,Xmlhttprequest,Selectsinglenode,Selectnodes,Javascript: var req=xmlDoc.responseXML.selectSingleNode("//title"); alert(req.text); 与预期一样,返回第一个“title”节点的文本 但是这个 var req=xmlDoc.responseXML.selectNodes("//title"); alert(req.text); 返回“未定义”。返回以下内容: var req=xmlDoc.responseXML.selectNodes("//title").

Javascript:

var req=xmlDoc.responseXML.selectSingleNode("//title");
alert(req.text);
与预期一样,返回第一个“title”节点的文本

但是这个

var req=xmlDoc.responseXML.selectNodes("//title");
alert(req.text);
返回“未定义”。返回以下内容:

var req=xmlDoc.responseXML.selectNodes("//title").length;
alert(req);
返回“2”,我不明白。可能当我选择Nodes时,标题中没有文本节点。这是我现在的猜测……这是xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<decal>
<company>Victor</company>
<title>Wood Horn Blue Background</title>
<image>
<url>victor01.jpg</url>
<width>60</width>
<height>60</height>
<name>Wood Horn Blue Background</name>
<link></link>
</image>
<price>$15.00</price>
<instock>In Stock</instock>
<notes>no extra info</notes>
</decal>
<decal>
<company>Victor</company>
<title>Wood Horn without Black Ring</title>
<image>
<url>victor02.jpg</url>
<width>60</width>
<height>60</height>
<name>Wood Horn Without Black Ring</name>
<link></link>
</image>
<price>$15.00</price>
<instock>In Stock</instock>
<notes>no extra info</notes>
</decal>
</catalog>

胜利者
木角蓝底
victor01.jpg
60
60
木角蓝底
$15.00
有现货的
没有额外的信息
胜利者
无黑圈木喇叭
victor02.jpg
60
60
无黑圈木喇叭
$15.00
有现货的
没有额外的信息

谢谢

选择节点
返回一个数组

因此,当您编写
var req=xmlDoc.responseXML.selectNodes(“//title”)
时,
req
变量包含一个元素数组。
由于数组没有
text
属性,因此将得到
undefined


相反,您可以编写
req[0]。text
以获取数组中第一个元素的文本。

正如方法名称所示,
selectNodes
返回一个集合(数组)。你需要把它们绕过去。或者,如果确定结构,请抓取第一个元素。

selectNodes返回一个数组而不是单个节点(因此方法的复数命名)

您可以使用索引器获取各个节点:

var req=xmlDoc.responseXML.selectNodes("//title");
for (var i=0;i<req.length;i++) {
   alert(req[i].text);
}
var req=xmlDoc.responseXML.selectNodes(//title”);

对于(var i=0;i基本上,我需要每个文件中的文本列表。很抱歉,此处刷新速度不是很快。我需要每个文件中的文本列表。好的,它工作了,但我必须将其设置为“iMy bad”-为了完整性,它应该有req.length。我必须键入错误。我现在要编辑。