我正在解析XML作为对象并通过标记名访问节点,我有一个问题,我希望看到更新的值不会更新。警报显示了值,它是正确的。但我需要它显示在文档上,而不是它。
var x = xml.responseXML;
var v1 = document.getElementById("sid");
alert(x.getElementsByTagName("ID")[0].childNodes[0].nodeValue);
v1.innerText = x.getElementsByTagName("ID")[0].childNodes[0].nodeValue;
我还有一个问题,如何允许编辑/突出显示节点?
如果nodeValue
是null
,设置它的值没有效果(来自the docs)。但是,您可以通过其他方式修改XML内容,例如.innerHTML
,.innerText
,.value
等。 .innerHTML
的示例:
// creates a Document, as in XMLHttpRequest.responseXML
const docText = `
<!DOCTYPE html>
<body>
<div>Hello</div>
<div>World</div>
</body>`;
const doc = (new DOMParser()).parseFromString(docText, 'application/xml');
// setting nodeValue over null has no effect...
console.log(doc.getElementsByTagName('div')[0].nodeValue);
doc.getElementsByTagName('div')[0].nodeValue = 'Bye';
console.log(doc.getElementsByTagName('div')[0].nodeValue);
// ...but you can modifies the XML in different ways
console.log(doc.getElementsByTagName('div')[0].innerHTML);
doc.getElementsByTagName('div')[0].innerHTML = 'Bye';
console.log(doc.getElementsByTagName('div')[0].innerHTML);