无法访问动态添加的节点到XML对象

时间:2018-08-17 15:30:37

标签: javascript

我在JavaScript中有一个XML对象 xmlObj (以ajax响应返回),如下所示-

<books>
    <book title="Dork Diaries" author="Rachel Renee Russell"></book>
    <book title="Dragon in the Tunnel" author="Ruskin Bond"></book>
    <book title="The Fox" author="Frederick Forsyth"></book>
</books>

我正在尝试动态添加这样的节点-

var _tempNode = document.createElement("book");
_tempNode.setAttribute("title", "Captive");
_tempNode.setAttribute("author", "Tony Park");
xmlObj.appendChild(_tempNode);

当我在Chrome中检查元素时,可以看到更新后的xmlObj为-

<books>
    <book title="Dork Diaries" author="Rachel Renee Russell"></book>
    <book title="Dragon in the Tunnel" author="Ruskin Bond"></book>
    <book title="The Fox" author="Frederick Forsyth"></book>
    <book title="Captive" author="Tony Park"></book>
</books>

但是当我尝试选择新添加的节点时,它将返回NULL

xmlObj.querySelector('book[title="Captive"]')) // NULL

但是,这将返回有效节点-

xmlObj.querySelector('book[title="Dork Diaries"]')) // <book title="Dork Diaries" author="Rachel Renee Russell"></book>

知道如何访问新添加的节点吗?

0 个答案:

没有答案