標籤:button dom 11.2 script react event cdn name const
####react最基礎的文法和不依賴環境的純前端免編譯代碼
參照:http://www.ruanyifeng.com/blog/2015/03/react.html
注意事項:1.必須放倒伺服器上,在檔案系統上無法運行
login.html
<!doctype html><head> <meta charset="utf-8"> <script src="https://npmcdn.com/[email protected]/dist/react-with-addons.min.js"></script> <script src="https://npmcdn.com/[email protected]/dist/react-dom.min.js"></script> <script src="https://npmcdn.com/[email protected]/browser.min.js"></script> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css"> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap-theme.min.css"> <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script> <script src="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <link rel="stylesheet" href="login.css" type="text/css" /> <title> 頁面 </title></head><body style="margin: auto"><div id="root"></div><script type="text/babel" src="login.js"></script></body></html>
login.css
.login{ background-color: red;}.header{ height: 30px; background-color: gray;}.userRole{ height:80px; background-color: lightcyan;}.userId{}.userPassword{}.submitButtonEnabled{ color:blue;}.submitButtonDisabled{ color: gray;}
login.js
class Login extends React.Component{ //props的類型和是否必須填寫// static propTypes = {// }; constructor(props) { super(props); this.state = { userId: ‘‘, userPassword: ‘‘, submitEnabled: false, }; } componentWillMount() { console.log("componentWillMount"); } componentDidMount() { console.log("componentDidMount"); } componentWillUnmount() { console.log("componentWillUnmount"); } //event handleClickSubmit() { if(this.state.userId.length < 1 || this.state.userPassword.length < 1){ return; } $.get("http://publicschool.sinaapp.com/test/test.php?name=jack", function(result) { console.log(result); }); } handleChangeId () { this.setState({ userId:this.refs._ref_userId.value, submitEnabled:this.refs._ref_userId.value.length > 0 && this.refs._ref_userPassword.value.length > 0, }); } handleChangePassword () { this.setState({ userPassword:this.refs._ref_userPassword.value, submitEnabled:this.refs._ref_userId.value.length > 0 && this.refs._ref_userPassword.value.length > 0, }); } //渲染方法 render () { return( <div className="login"> <div className="header"> {this.props.title} </div> <div className="userRole"> {this.props.role} </div> <input className="userId" type="text" ref="_ref_userId" placeholder="使用者名稱" onChange={()=>this.handleChangeId()} /> <input className="userPassword" type="password" ref="_ref_userPassword" placeholder="密碼" onChange={()=>this.handleChangePassword()} /> <button type="button" onClick={()=>this.handleClickSubmit()} className={this.state.submitEnabled?"submitButtonEnabled":"submitButtonDisabled"}>登入</button> </div>); }}ReactDOM.render( <Login title="title" role="ddd"/>, document.getElementById(‘root‘));
react純前端不依賴於打包工具的代碼