Javascript React-js 忽略标签的“for”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29161330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:56:22  来源:igfitidea点击:

React-js ignores label's 'for' attribute

javascriptreactjs

提问by mike01010

I know that for 'class' we must use className, but how do i get react to preserve 'for' attribute?

我知道对于 'class' 我们必须使用 className,但是我如何对保留 'for' 属性做出反应?

The following:

下列:

<label for="recipient-name" className="control-label">Recipient:</label>

is rendered as:

呈现为:

<label class="control-label">Recipient:</label>

on an unrelated note, i find it annoying that i can not change attributes using chrome's console when using React. is there a way around that? for example if i inspect the rendered element and add the 'for' attribute manually, it disappears when i click away from that control (presumably because react re-renders the control i'm guessing)

在一个不相关的注释中,我发现在使用 React 时无法使用 chrome 的控制台更改属性很烦人。有没有办法解决这个问题?例如,如果我检查渲染的元素并手动添加“for”属性,当我点击远离该控件时它会消失(大概是因为 react 重新渲染了我猜测的控件)

回答by zerkms

You must use htmlForattribute instead

您必须改用htmlFor属性

<label htmlFor="recipient-name" className="control-label">Recipient:</label>

回答by Ali Rehman

foris a keyword in javascript so in JSX you can't use it. You must use htmlForwhich is translated into forattribute once it is rendered to the DOM.

for是 javascript 中的关键字,因此在 JSX 中您不能使用它。一旦渲染到 DOM,您必须使用htmlForwhich 被翻译成for属性。