JSX(JavaScriptXML)語法入門:css
JSX規範:html
例子react
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>react筆記</title>
<script src="js/react.min.js"></script>
<script src="js/react-dom.min.js"></script>
<script src="js/browser.js"></script>
</head>
<body>
<div id="demo1"></div>
<script type="text/babel">
var oDemo = document.getElementById("demo1"),
Demo = React.createClass({
change(){
return "demo";
},
handleClick(){
alert(1);
},
render(){
//this指向整個組件
return <div className="demo" onClick={this.handleClick}>這是一個{this.change()}</div>
}
});
//渲染
ReactDOM.render(<Demo/>, oDemo);
</script>
</body>
</html>
複製代碼
使用{/* */}註釋瀏覽器
<div id="demo2"></div>
<script type="text/babel">
var oDemo = document.getElementById("demo2"),
HelloWorld = React.createClass({
render(){
// 如今這裏是屬於js的部分,不屬於JSX語法的部分
return (
<div className="box" // class名字>
{/*這是一個標題*/}
<h1 className="title">Hello World</h1>
{/*這是說明*/}
<p>你好世界!</p>
<div className="box2">你好</div>
</div>
)
}
})
ReactDOM.render(<HelloWorld/>, oDemo);
</script>
複製代碼
組件的樣式:bash
注意事項:在HTML5中與在React中樣式書寫區別babel
例子dom
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<script src="js/react.min.js"></script>
<script src="js/react-dom.min.js"></script>
<script src="js/browser.js"></script>
</head>
<body>
<div id="demo3"></div>
<script type="text/babel">
var oDemo = document.getElementById("demo3"),
HelloWorld = React.createClass({
render(){
var styles = {
color: 'blue',
fontSize: '30'
};
return (
<div className="box">
<h3 className="title" style={{color:'red',backgroundColor:'lime'}}>行內樣式</h3>
<p className="subtitle" style={styles}>對象樣式</p>
<p className="details">css樣式</p>
</div>
);
}
});
ReactDOM.render(<HelloWorld/>, oDemo);
</script>
</body>
</html>
複製代碼
//demo4
<div id="demo4"></div>
<script type="text/babel">
var oDemo = document.getElementById("demo4");
Demo = React.createClass({
render:function(){
var text = {
__html:'<span>我是在div中的span標籤</span>'
};
return (<div>
<div dangerouslySetInnerHTML={text}></div>
</div>);
}
});
ReactDOM.render(<Demo/>, oDemo);
複製代碼