标签:
1 React.render( 2 <ezlampcomp onoff="off"></ezlampcomp> , 3 document.querySelector("#content"));
1 var myOnoff = "on"; 2 React.render( 3 <ezlampcomp onoff="{myOnoff}"></ezlampcomp>, 4 document.querySelector("#content"));
1 var myOnoff = "on"; 2 React.render( 3 React.createElement( 4 EzLampComp, 5 { 6 onoff : myOnoff 7 } 8 ), 9 document.querySelector("#content") 10 );
1 <style> 2 .ez-lamp{ 3 display : inline-block; 4 margin : 5px; 5 width : 30px; 6 height : 30px; 7 border-radius : 50%; 8 } 9 .ez-lamp.on{ 10 opacity : 1; 11 background : -webkit-radial-gradient(30% 30%,white 5%,red 95%); 12 } 13 .ez-lamp.off{ 14 opacity : 0.5; 15 background : -webkit-radial-gradient(30% 30%,#888 5%,red 95%); 16 } 17 </style>
1 <script type = "text/jsx"> 2 //定义React组件 3 var EzLampComp = React.createClass({ 4 render : function(){ 5 6 //取得属性值 7 var onoff = this.props.onoff; 8 9 //返回React元素 10 if(onoff == "on") 11 return <span className = "ez-lamp on"></span>; 12 else 13 return <span className = "ez-lamp off"></span>; 14 } 15 }); 16 17 var myOnoff = "on"; 18 19 setInterval(function() { 20 21 //渲染React元素 22 React.render( 23 <EzLampComp onoff={myOnoff}/> , 24 document.querySelector("#content")); 25 26 myOnoff = myOnoff == "on" ? "off" : "on"; 27 28 }, 1000); 29 30 31 </script>
1 //HTML 2 <div style=“borderRadius:50%;height:200px;width:200px"></div>
1 var myStyle = { 2 borderRadius:”50%", 3 width:"200px", 4 height:"200px" 5 }; 6 //JSX 7 var e = <div style="{myStyle}"></div>; 8 //JavaScript 9 var e = React.createElement( 10 "div",{ 11 style : myStyle 12 } 13 ); 14 //render 15 React.render(e,...);
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>ex15:EzLampComp</title> 6 <script src="lib/react.js"></script> 7 <script src="lib/JSXTransformer.js"></script> 8 <style> 9 .ez-lamp{ 10 display : inline-block; 11 margin : 5px; 12 width : 30px; 13 height : 30px; 14 } 15 </style> 16 </head> 17 <body> 18 <div id="content"></div> 19 <script type = "text/jsx"> 20 //定义React组件 21 var EzLampComp = React.createClass({ 22 render : function(){ 23 //取得属性值 24 var color = this.props.color, 25 onoff = this.props.onoff; 26 //亮光颜色 27 var lights = { 28 "off":"#888", 29 "on":"#fff" 30 }; 31 //透明度 32 var opacity ={ 33 "off":0.5, 34 "on":1.0 35 }; 36 //根据属性设置附加的样式 37 var style = { 38 borderRadius : "50%", //对应样式:border-radius 39 opacity : opacity[this.props.onoff], 40 background : "-webkit-radial-gradient(30% 30%," + lights[onoff] + " 5%," + color +" 95%)" 41 }; 42 //返回React元素 43 return <span className="ez-lamp" style={style}></span>; //JSX 44 } 45 }); 46 //渲染React元素 47 React.render( 48 <div> 49 <EzLampComp color="green" onoff="off"/> 50 <EzLampComp color="green" onoff="on"/> 51 <EzLampComp color="red" onoff="off"/> 52 <EzLampComp color="red" onoff="on"/> 53 <EzLampComp color="blue" onoff="off"/> 54 <EzLampComp color="blue" onoff="on"/> 55 </div> 56 ,document.querySelector("#content")); 57 </script> 58 </body> 59 </html>
标签:
原文地址:http://www.cnblogs.com/galenyip/p/4573989.html