[英]Creating DOM element from XML string, how to use .getElementsByName()?
因此,我嘗試將通過XML字符串創建的DOM元素插入文檔中。 但是首先,我試圖修改該字符串中某些元素的innerText。 我正在嘗試使用以下內容...
refElem = document.createElement('div');
refElem.innerHTML = xmlString;
...
newChild = document.createElement('div');
newChild.innerHTML = refElem.innerHTML;
newChild.getElementsByName('title') ...
但我收到以下錯誤:
main.js:140 Uncaught TypeError: newChild.getElementsByName is not a function
有人知道如何將xml字符串解析為DOM元素並在將該元素插入到頁面中之前對其進行操作嗎?
沒有jQuery的答案,請! 僅Vanilla JS!
getElementsByName
和getElementByID
是DOM文檔對象的功能,而不是DOM元素對象的功能。 要搜索DOM元素對象,請使用querySelector
或`querySelectorAll。
例如:
newChild.querySelectorAll('#title');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.