React给添加元素增加样式

React给添加元素增加样式


第一种方法:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>React</title>
	<script src="http://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.runoob.com/assets/react/browser.min.js"></script>
    <script src="lib/style.js"></script>
    <style>
        .lala{
        	color:red;
            font-size: 40px;
        	}
    </style>
</head>
 <body>
    <div id="example"></div>
    <script type="text/babel">
    var Hello=React.createClass({
    	render:function(){
    		return (
				<div className="lala"> 我出来了!
				</div>
    		)
    	}
    })
      ReactDOM.render(
        <Hello/>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>


第二种方法:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>React</title>
	<script src="http://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.runoob.com/assets/react/browser.min.js"></script>
    <script src="lib/style.js"></script>
  
</head>
 <body>
    <div id="example"></div>
    <script type="text/babel">
    var Hello=React.createClass({
    	render:function(){
        var style={color:"red","font-size":"40px"}
    		return (
				<div style={style}> 我出来了!
				</div>
    		)
    	}
    })
      ReactDOM.render(
        <Hello/>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>


第三种方法:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>React</title>
	<script src="http://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.runoob.com/assets/react/browser.min.js"></script>
    <script src="lib/style.js"></script>
  
</head>
 <body>
    <div id="example"></div>
    <script type="text/babel">
    var Hello=React.createClass({
        render:function(){
            return (
                <div style={this.css.style3} > 我出来了!
                </div>
            )
        }
    })
    Hello.prototype.css={
        style3:{color:"green",fontSize:"50px",background:"#ccc"}
    }
      ReactDOM.render(
        <Hello/>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>


第四种方法:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>React</title>
	<script src="http://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>
    <script src="http://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>
    <script src="http://static.runoob.com/assets/react/browser.min.js"></script>
    <script src="lib/style.js"></script>
  
</head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
    var Hello=React.createClass({
        render:function(){
            return (
                <div style={style.str1} > 我出来了!
                </div>
            )
        }
    })
      ReactDOM.render(
        <Hello/>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>
style.js的代码:

var style={
	 str1:{"font-size":"40px",color:"green",width:"200px",height:"300px","text-align":"center",border:"1px solid gray"}
}
不提倡使用style样式及行间样式,使用外部js的方式或者原型链的比较好.


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值