我需要在下面的 XML 代码中<title>
替换为。<sec-title>
但结果<named-content>
从<title>
.
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>Teting<named-content content-type='index'></named-content></title>" +
"<author>Giada De Laurentiis<named-content content-type='index'></named-content></author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
var t = xmlDoc.querySelector('title');
var newElement = xmlDoc.createElement('sec-title');
var t_clone = t.cloneNode(true);
var t_content = t_clone.childNodes[0];
newElement.appendChild(t_content);
t.parentNode.insertBefore(newElement, t);
t.parentNode.removeChild(t);
<title>
被替换,但<named-content>
标签消失了。
我需要:
<sec-title>Testing<named-content content-type='index'></named-content></sec-title>