简体   繁体   English

如何在javascript中更改对元素的引用?

[英]How can i change a reference to an element in javascript?

I would like to modify an element of my xml. 我想修改我的xml元素。 I have to query it to find it, and then I only seem to be able to modify a copy of it. 我必须查询它才能找到它,然后我似乎只能修改它的副本。 I use: clientXML.querySelector("[id=XMLIssue-9]").childNodes[2].textContent; 我使用: clientXML.querySelector("[id=XMLIssue-9]").childNodes[2].textContent; to isolate the element in question. 隔离有问题的元素。 I would like to just replace the element with this one newXMLIssue.childNodes[0] . 我想用这个newXMLIssue.childNodes[0]替换元素。 They both have the exact same structure. 它们都具有完全相同的结构。 I cant do: 我不能这样做:

clientXML.querySelector("[id=XMLIssue-9]") = newXMLIssue.childNodes[0];

because it causes an error to have the function on the left side of the equation. 因为它会导致错误,使函数位于等式的左侧。 I also tried: 我也尝试过:

var x = clientXML.querySelector("[id=XMLIssue-9]"); 
x = newXMLIssue.childNodes[0];

but this only changes a copy of the element. 但这只会更改元素的副本。

You have to call replaceChild on the parent: 您必须在父级上调用replaceChild

var parent = clientXML.querySelector("[id=XMLIssue-9]");

parent.replaceChild(parent.childNodes[0], parent.childNodes[2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM