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

查看:30
本文介绍了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:

e.g. the following JSX is used:

<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天全站免登陆