我用html编写了这段代码
<select id ='Font_Size' onchange="ChangeFont()">
<option>Font Size </option>
<option id ='sizeUp'>Large </option>
<option id ='normal'>Normal</option>
<option id ='sizeDown'>Small</option>
</select>
React.createElement('input',{type:'select',onChange:this.ChangeFont},React.createElement("input",{type:"Option"}))
但是我想创建一个react元素,它将使用功能React.createElement()创建一个选择字段,并按照上面的代码显示它。底部的代码是我尝试过的,但是不起作用
答案 0 :(得分:1)
您要创建的是一个<input/>
,您想创建一个<select/>
。运行以下代码片段以查看运行中的代码:
var App = React.createClass({
render() {
return React.createElement("select", {},
React.createElement("option", {value: "A"}, "Option A"),
React.createElement("option", {value: "B"}, "Option B"),
React.createElement("option", {value: "C"}, "Option C")
);
}
});
ReactDOM.render(<App label="this is the label prop" />, document.getElementById('react'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.6.2/react-dom.min.js"></script>
<div id="react"></div>