【问题标题】:How to update a parsed xml value如何更新解析的 xml 值
【发布时间】:2019-09-13 03:06:36
【问题描述】:

我正在将 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;

我还有一个问题是如何允许编辑/突出显示节点?

【问题讨论】:

  • have a problem where the value I'd like to see updated doesn't get updated. 这是什么意思
  • @zero298 对,我犯了一个错误,改成 getElementById 还是一样的问题。
  • @srknzl 我已经制作了一个名为 sid 的 td 元素,它目前是空的,但我需要它至少显示第一个元素。我先做了解析,根据我解析的内容我需要 v1(sid) 来改变
  • v1.innerText = "foobar" 有效吗?对任何节点的任何更新都有效吗?

标签: javascript html ajax xml dom


【解决方案1】:

如果nodeValuenull,则设置其值无效(来自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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多