1、使用jsx创建虚拟DOM
const vdom = <h1 id="title" className="red"><span>helloReact</span></h1> /*此处一定不要写引号*/
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8">
<!-- 移动端适配--><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>1_使用jsx创建虚拟DOM</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<!--引入babel,用于jsx的转换jsx-->
<script type="text/javascript" src="../js/babel.min.js"></script>
<script type="text/babel">/*此处一定要写babel*///1.创建虚拟DOMconst vdom = <h1 id="title" className="red"><span>helloReact</span></h1> /*此处一定不要写引号*///2.渲染虚拟DOM到页面ReactDOM.render(vdom,document.getElementById('root'))
</script>
</body>
</html>
2、使用js创建虚拟DOM
const vdom = React.createElement('h1',{id:'title',className:'red'},React.createElement('span',{},'hello,react'))
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8">
<!-- 移动端适配--><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>2_使用js创建虚拟DOM</title>
</head>
<body>
<!--准备一个容器-->
<div id="root"></div>
<!--引入react核心库-->
<script type="text/javascript" src="../js/react.development.js"></script>
<!--引入react-dom.js库,用于支持react操作DOM-->
<script type="text/javascript" src="../js/react-dom.development.js"></script>
<script type="text/javascript">//1.创建虚拟DOMconst vdom = React.createElement('h1',{id:'title',className:'red'},React.createElement('span',{},'hello,react'))//2.渲染虚拟DOM到页面ReactDOM.render(vdom,document.getElementById('root'))
</script>
</body>
</html>
jsx创建虚拟DOM更加方便代码量更少