【问题标题】: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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-16
      • 2018-03-07
      • 2023-03-23
      • 1970-01-01
      • 2017-10-09
      • 2023-04-08
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多