綁定屬性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
    <script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>

</head>
<body>
    <div id="app"></div>
    
    <script type="text/babel">
        // 封裝App 組件
        class App extends React.Component{ 
            constructor(){
                super(); 
                this.state = {
                   title:'標題', 
                   //    imagUrl:'https://himg.bdimg.com/sys/portrait/item/wise.1.7410f781.uIX3cBSrpKoNZuTngAm-Sg.jpg?time=8674'
                   imagUrl:"" ,
                   active: true
                } 
            }

            render(){
                const {title,imagUrl,active} =  this.state;
                return ( 
                    <div> 
                        <h2 title={title}>我是標題</h2>
                        <img src={imagUrl} alt="" /> 
                        <div className="box title">我是div元素</div>
                        <div className={"box title " + (active ? 'active' : '')}>我也是div元素</div>
                    
                        <div style={{color:"red",fontSize:50}}>我是div元素,綁定style樣式</div>
                    </div> 
                )
            }  
        }
 
        ReactDOM.render(<App/>,document.getElementById('app')); 
    </script>
</body>
</html>
相關文章
相關標籤/搜索