首页 前端知识 实现一个在线编辑html,css,js代码的功能

实现一个在线编辑html,css,js代码的功能

2024-08-12 10:08:27 前端知识 前端哥 433 973 我要收藏

实现一个在线编辑html,css,js代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Online HTML/CSS/JS Editor</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            height: 100vh;
            margin: 0;
        }

        .editor-container {
            display: flex;
            flex: 1;
            padding: 10px;
        }

        .editor {
            flex: 1;
            margin: 5px;
            display: flex;
            flex-direction: column;
        }

        textarea {
            flex: 1;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
            resize: none;
        }

        #result {
            flex: 1;
            margin: 5px;
            border: 1px solid #ccc;
            overflow: auto;
            /* 添加滚动条以便显示输出 */
        }

        .header {
            padding: 10px;
            background: #f1f1f1;
            border-bottom: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <div class="header">
        <button id="runButton">Run</button>
    </div>
    <div class="editor-container">
        <div class="editor">
            <h3>HTML</h3>
            <textarea id="htmlCode"><h1 id="text">Hello, World!</h1></textarea>
        </div>
        <div class="editor">
            <h3>CSS</h3>
            <textarea id="cssCode">h1 { color: blue; }</textarea>
        </div>
        <div class="editor">
            <h3>JavaScript</h3>
            <textarea id="jsCode">
                console.log('Hello, World from iframe!');
            </textarea>
        </div>
        <iframe id="result"></iframe>
    </div>

    <script>
        const runCode = () => {
            try {
                const htmlCode = document.getElementById('htmlCode').value;
                const cssCode = document.getElementById('cssCode').value;
                let jsCode = document.getElementById('jsCode').value;
                jsCode = jsCode.replace(/console\.log\((.*?)\);?/g, "window.parent.displayOutput($1);");

                const resultFrame = document.getElementById('result');
                let iframeDoc = resultFrame.contentDocument || resultFrame.contentWindow.document;

                iframeDoc.head.innerHTML = `<style>${cssCode}</style>`;
                iframeDoc.body.innerHTML = htmlCode;
                const script = iframeDoc.createElement('script');
                script.textContent = jsCode
                iframeDoc.body.appendChild(script);
            } catch (error) {
                console.error("Error:", error);
            }
        };

        document.getElementById('runButton').addEventListener('click', runCode);

        // 在父窗口中显示输出的函数
        function displayOutput(output) {
            const outputArea = document.getElementById('output');
            outputArea.textContent += output + '\n';
        }
    </script>
    <!-- 显示输出的区域 -->
    <div class="header">
        <h3>Output</h3>
    </div>
    <div id="output" style="overflow: auto; height: 100px;"></div>
</body>

</html>
转载请注明出处或者链接地址:https://www.qianduange.cn//article/15350.html
标签
评论
发布的文章

jQuery3 学习手册(三)

2024-08-18 22:08:04

vue和jQuery有什么区别

2024-04-29 11:04:47

推荐项目:jQuery.Gantt

2024-08-18 22:08:37

jQuery UI 秘籍(一)

2024-08-18 22:08:15

jQuery详解

2024-04-29 11:04:38

echarts饼图点击图例问题

2024-08-18 22:08:48

echarts天气折线图

2024-08-18 22:08:46

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!