【问题标题】:Instead of "for" in label what can i use?我可以使用什么来代替标签中的“for”?
【发布时间】:2014-09-19 17:06:09
【问题描述】:

我正在努力在 React.js 中创建一个切换按钮,

我正在动态创建输入标签,

所以当我要在标签中申请属性时,它不起作用。

请帮我解决 JS 中的任何其他属性

render: function() {
    var checks = this.state.data.map(function(d) {
        return (
            <div className="switch">
                {d.id}
                <input id="toggle-1" className="toggle toggle-round" type="checkbox" checked={d.selected} onChange={this.__changeSelection.bind(this, d.id)} />
                <label for="toggle-1"></label>
            </div>
        );

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    看看source code,似乎像class(您需要使用className)一样,您需要使用备用名称——在这种情况下为htmlFor

    <label htmlFor="toggle-1"></label>
    

    可能是因为:

    • both class and for are reserved keywords in JavaScript
    • 在这少数情况下,属性名称不会与暴露给 JavaScript 的名称一一对应,React 选择匹配 JavaScript API,而不是导入 DOM 之一(React 处理 data-*aria-* 转换,但文档明确指出了这种行为)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      相关资源
      最近更新 更多