简体   繁体   English

从XML字符串创建DOM元素,如何使用.getElementsByName()?

[英]Creating DOM element from XML string, how to use .getElementsByName()?

So I'm trying to insert a DOM element created from an XML string into my document. 因此,我尝试将通过XML字符串创建的DOM元素插入文档中。 But first, I am trying to modify the innerText of some of the elements in that string. 但是首先,我试图修改该字符串中某些元素的innerText。 I'm trying to use the following... 我正在尝试使用以下内容...

refElem = document.createElement('div');
refElem.innerHTML = xmlString;
...
newChild = document.createElement('div');
newChild.innerHTML = refElem.innerHTML;
newChild.getElementsByName('title') ...

But I'm getting the following error: 但我收到以下错误:

main.js:140 Uncaught TypeError: newChild.getElementsByName is not a function

Anyone know how I can parse an xml string into a DOM element and manipulate that element before inserting it into my page? 有人知道如何将xml字符串解析为DOM元素并在将该元素插入到页面中之前对其进行操作吗?

NO jquery answers please! 没有jQuery的答案,请! Vanilla JS only! 仅Vanilla JS!

getElementsByName and getElementByID are functions of DOM document object, not DOM element object. getElementsByNamegetElementByID是DOM文档对象的功能,而不是DOM元素对象的功能。 To Search on a DOM element object, use querySelector or `querySelectorAll. 要搜索DOM元素对象,请使用querySelector或`querySelectorAll。

For example: 例如:

newChild.querySelectorAll('#title');

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

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