首页 > 解决方案 > 如何在同一组件内由 React 组件渲染的 DOM 中替换一组 html 元素代替子元素

问题描述

我想通过反应组件替换一组元素说“..”来代替已经呈现的 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>

请指导我如何使这成为可能。提前致谢!

标签: reactjssharepoint-onlinespfx

解决方案


您可以使用ReactDOM.render()替换或更新元素。

  • https://codepen.io/vliumang/pen/XWjGVev

或者直接通过id改变div:

document.getElementById("3").innerHTML = "New text!";

BR


推荐阅读