我想將 parsedHtml 顯示到元素中。但在顯示它之前,我應該將一些 html 更改為 parsedHtml。所以我用replace來代替這個類似的代碼。但它有TypeError: (intermediate value).parseFromString(...).replace is not a function問題。你能幫我解決這個問題嗎?謝謝
const parsedHTML = new DOMParser().parseFromString(html, "text/html");
const renderHtml = parsedHTML.replace('aria-expanded="false"', 'aria-expanded="true"');
console.log(renderHtml);
uj5u.com熱心網友回復:
您將 DOMParser 物件視為字串。這是行不通的。
您需要做這樣的事情,因為不建議您在決議之前更改有效的 HTML
const html = `<html><div aria-expanded="false"></div></html>`
const parsedHTML = new DOMParser().parseFromString(html, "text/html");
parsedHTML.querySelector("div").setAttribute("aria-expanded",true)
console.log(parsedHTML.querySelector("div"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/381748.html
標籤:javascript dom 类型错误
下一篇:解決bcrypt回傳承諾
