selectSingleNode工作但不是selectNodes

Tro*_*roy 2 javascript internet-explorer xmlhttprequest selectnodes selectsinglenode

使用Javascript:

var req=xmlDoc.responseXML.selectSingleNode("//title");
alert(req.text);
Run Code Online (Sandbox Code Playgroud)

按预期,返回第一个"标题"节点的文本.

但是这个

var req=xmlDoc.responseXML.selectNodes("//title");
alert(req.text);
Run Code Online (Sandbox Code Playgroud)

返回"未定义".下列:

var req=xmlDoc.responseXML.selectNodes("//title").length;
alert(req);
Run Code Online (Sandbox Code Playgroud)

返回"2" 我不明白.也许当我选择节点时,它没有获得标题内的文本节点.这是我现在的猜测......这是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>
Run Code Online (Sandbox Code Playgroud)

谢谢

SLa*_*aks 5

selectNodes 返回一个数组.

因此,在编写时var req=xmlDoc.responseXML.selectNodes("//title"),req变量包含一个元素数组.
因为数组没有text属性,所以你得到了undefined.

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