【问题标题】:How to replace a set of html elements in place of a child element within the DOM rendered by React component within same component如何在同一组件内由 React 组件渲染的 DOM 中替换一组 html 元素代替子元素
【发布时间】:2021-01-20 10:19:47
【问题描述】:
我想通过 react 组件替换一组元素,比如“..”来代替已经呈现的 DOM 中的元素。
假设 React 渲染的 html 如下所示
<p>This is a demo</p>
<div id='1'>Hello<div id='2'><div id='3'>I want to replace this third div with another set of element</div></div></div>
请指导我如何使这成为可能。提前致谢!
【问题讨论】:
标签:
reactjs
sharepoint-online
spfx
【解决方案1】:
您可以使用ReactDOM.render() 替换或更新元素。
- https://codepen.io/vliumang/pen/XWjGVev
或者直接通过id改变div:
document.getElementById("3").innerHTML = "New text!";
BR