如何find特定的父节点?
我必须将docx转换为XML并使用DOM来查找特定的父节点。
例如
<chapter> <title> <label>Chapter 1 </label> </title> <para>This is chapterpara <a id="book"/> </para> </chapter>
在这里我想find锚节点的超级父节点的孩子(标题节点)。
通过获取我知道锚节点引用
var itemanchor=ItemElement.getElementsByTagName('a');
如何从上面的引用遍历特定的父节点?
提前致谢。
使用需要使用createDocumentFragment()
方法来创build假文档,然后将您的HTML追加到假文档,并从中获取目标元素。 看到这个例子
var HTML = '<chapter>\ <title>\ <label>Chapter1</label>\ </title>\ <para>\ This is chapterpara\ <a id="book" href="mySiteAddress" />\ </para>\ </chapter>'; var element = document.createElement('div'); element.innerHTML = HTML; var docfrag = document.createDocumentFragment(); docfrag.appendChild(element); var href = docfrag.getElementById("book").getAttribute("href"); document.write(href);