【发布时间】:2018-07-16 18:46:15
【问题描述】:
我正在使用 React js,并且正在构建大量重复的元素,这些元素都被包裹在同一个 html 中,如下所示:
<div>
<div class="child">
<div class="grand-child">
<Element1 />
</div>
</div>
<div class="child">
<div class="grand-child">
<Element2 />
</div>
</div>
<div class="child">
<div class="grand-child">
<Element3 />
</div>
</div>
</div>
不必不断地将每个元素包装在 'child' 和 'grand-child' div 中,有没有更简单的方法可以编写这样我不必重复自己?
我研究了类似innerHTML 属性的东西,它标记了一个html 元素并在该原始元素中插入信息/元素。我想做的恰恰相反,而是将我的原始元素与其他 html 元素一起包装,但似乎 outerHTML 属性不以这种方式运行。
有什么方法可以包装 html 元素,如下面的这个伪解决方案所示?谢谢。
Let <foo></foo> =
<div class="child">
<div class="grand-child">
</div>
</div>
<div class="parent">
<foo>
<p>This is the first element</p>
</foo>
<foo>
<p>This is the second element</p>
</foo>
<foo>
<p>This is the third element</p>
</foo>
</div>
【问题讨论】:
标签: javascript jquery html reactjs