React忽略了标签元素的'for'属性 [英] React ignores 'for' attribute of the label element

查看:115
本文介绍了React忽略了标签元素的'for'属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在React(Facebook的框架)中,我需要使用标准的for属性呈现一个绑定到文本输入的标签元素.

In React (Facebook's framework), I need to render a label element bound to a text input using the standard for attribute.

例如使用以下JSX:

<label for="test">Test</label>
<input type="text" id="test" />

但是,这会导致HTML缺少必需的(和标准的)for属性:

However, this produces HTML missing the required (and standard) for attribute:

<label>Test</label>
<input type="text" id="test">

我在做什么错了?

推荐答案

为了与DOM属性API保持一致,将for属性称为htmlFor.如果您使用的是React的开发版本,则应该在控制台中看到有关此的警告.

The for attribute is called htmlFor for consistency with the DOM property API. If you're using the development build of React, you should have seen a warning in your console about this.

这篇关于React忽略了标签元素的'for'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆