我想在 Html 中循环使用具有不同输入的函数 SliderComponent 以创建多个组件。我在网上找到了这个解决方案,我们使用“id”和“getElementById”构建字符串并以 Html 形式返回,但我无法让它工作。
export function GrommetButtonEx() {
return (
<div>
<Grommet theme={grommetTheme}>
<Sidebar
border={{ color: "grey", size: "medium" }}
width="340px"
background={{ color: "black", opacity: "strong" }}
style={{ borderRadius: "15px" }}
>
<Accordion style={{ color: "grey" }}>
<AccordionPanel
style={{ height: "30px" }}
label={
<Text color="white" weight="bold" size="small">
Joint Position
</Text>
}
>
<div id="output_div"></div>
{/* This works
<SliderComponent
sliderName={"slider 5"}
min={0}
max={17}
step={0.1}
/>
*/}
</AccordionPanel>
</Accordion>
</Sidebar>
</Grommet>
</div>
);
}
window.onload = function () {
var outputHTML = "";
for (var k = 0; k < 5; k++) {
outputHTML +=
'<SliderComponent sliderName={"slider ' +
k +
'"} min={0} max={17} step={0.1}';
}
document.getElementById("output_div").innerHTML = outputHTML;
};
我收到以下错误:
TypeError: Cannot set properties of null (setting 'innerHTML')
在:
document.getElementById("output_div").innerHTML = outputHTML;
这是正确的方法,还是有更好的方法?