dist/main.html

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <meta name="viewport" content="width=device-width">
    6. <title>JS Bin</title>
    7. </head>
    8. <body>
    9. <script src="./main.js">
    10. </script>
    11. </body>
    12. </html>

    // 实现解析jsx

    1. window.a = <div id="app" class="head">
    2. <span>1</span>
    3. <span>2</span>
    4. <span><h1>3</h1><h1>4</h1></span>
    5. hello
    6. </div>
    7. document.body.appendChild(window.a)
    8. function createElement(tagName, attributes, ...rest) {
    9. let element
    10. element = document.createElement(tagName)
    11. if (typeof attributes === 'object' && attributes instanceof Object) {
    12. for (const key in attributes) {
    13. element.setAttribute(key, attributes[key])
    14. }
    15. }
    16. for(const child of rest) {
    17. if (typeof child == 'string') {
    18. child = document.createTextNode(child)
    19. }
    20. element.appendChild(child)
    21. }
    22. return element
    23. }