Javascript reactjs - 如何设置背景颜色的内联样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31043436/
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
reactjs - how to set inline style of backgroundcolor?
提问by Duke Dougal
I want to set the style properties of some elements but I don't have the syntax correct. Can anyone suggest where I am wrong?
我想设置某些元素的样式属性,但语法不正确。谁能建议我错在哪里?
import React from 'react';
import debug from 'debug'
const log = debug('app:component:Header');
var bgColors = { "Default": "#81b71a",
"Blue": "#00B1E1",
"Cyan": "#37BC9B",
"Green": "#8CC152",
"Red": "#E9573F",
"Yellow": "#F6BB42",
};
export default class SideBar extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<a style="{{backgroundColor: {bgColors.Default}}}" >default</a>
<a style="{{backgroundColor: {bgColors.Blue}}}" >blue</a>
<a style="{{backgroundColor: {bgColors.Cyan}}}" >cyan</a>
<a style="{{backgroundColor: {bgColors.Green}}}" >green</a>
<a style="{{backgroundColor: {bgColors.Red}}}" >red</a>
<a style="{{backgroundColor: {bgColors.Yellow}}}" >yellow</a>
);
}
}
UPDATE: for anyone looking at this please see comments this is not working code.
更新:对于查看此内容的任何人,请参阅评论,这不是工作代码。
回答by Guilherme Medeiros
https://facebook.github.io/react/tips/inline-styles.html
https://facebook.github.io/react/tips/inline-styles.html
You don't need the quotes.
你不需要引号。
<a style={{backgroundColor: bgColors.Yellow}}>yellow</a>
回答by Craigo
Your quotes are in the wrong spot. Here's a simple example:
您的报价在错误的位置。这是一个简单的例子:
<div style={{backgroundColor: "#FF0000"}}>red</div>
回答by llioor
If you want more than one style this is the correct full answer. This is div with class and style:
如果你想要不止一种风格,这是正确的完整答案。这是具有类和样式的 div:
<div className="class-example" style={{width: '300px', height: '150px'}}></div>