V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
vfs
V2EX  ›  程序员

如何在 web 页面上实时预览 HTML CSS JS 效果

  •  
  •   vfs · 2 天前 · 1458 次点击

    因为最近在入门 html css javascript , 也会跟着书实践一下,代码都是很小的代码片段, 过后就丢弃了。 但是将来想用类似效果的时候就又忘记了,就想着在已有的 nextjs + react + tailwindcss 技术栈的网站上支持一下这个功能。

    看到好多网站都有类似的效果,但是由于本人是个前端小白,所以在这里请教一下各位大神,有没有好的方案,尽可能简单,毕竟它就是用来写一点点代码片段,预览一下效果的“小玩具”。

    感谢!

    第 1 条附言  ·  8 小时 16 分钟前
    感谢所有提供帮助的朋友。最终使用了 iframe 方案,在 豆包的帮助下完成。 感谢!
    17 条回复    2025-05-10 20:03:44 +08:00
    vfs
        1
    vfs  
    OP
       2 天前   ❤️ 1
    看到这个网站做的就非常好: https://playcode.io/html5 , 不知道做到这样有多大难度
    hausen
        2
    hausen  
       2 天前
    感觉这种用 cursor 能做
    wanf1020
        3
    wanf1020  
       2 天前
    最简单的 innerHTML
    rimworld
        4
    rimworld  
       2 天前
    iframe 不就可以
    radishzz
        5
    radishzz  
       2 天前
    嵌入 codepen ?
    molvqingtai
        6
    molvqingtai  
       2 天前   ❤️ 2
    要简单,一行代码就可以做到

    https://github.com/umpox/TinyEditor
    quietDown
        7
    quietDown  
       2 天前
    @molvqingtai 第一件见的这个,有点意思
    Plumbiu
        8
    Plumbiu  
       2 天前
    写过一篇博客: https://blog.plumbiu.top/posts/blog/implement-playground

    简单点,下面这行代码就能用

    ```ts
    import React, { createElement } from 'react'
    import { transform, Options } from 'sucrase'

    type Scope = Record<string, any>

    const transfromOptions: Options = {
    transforms: ['jsx', 'flow', 'imports'],
    }

    function evalCode(code: string, scope: Scope) {
    const _require = (k: keyof Scope) => {
    return scope[k]
    }
    const scopeKeys = Object.keys(scope)
    const scopeValues = scopeKeys.map((key) => scope[key])
    const _exports: Record<string, any> = {}
    const fn = new Function('exports', 'require', ...scopeKeys, code)
    fn(_exports, _require, ...scopeValues)
    return _exports.default
    }

    interface PlaygroundPreviewProps {
    code: string
    scope?: Scope
    }

    export function Preview({ code, scope }: PlaygroundPreviewProps) {
    const evalScope: Scope = {
    react: React,
    React: React,
    ...(scope ?? {}),
    }
    const mainCode = transform(code, transfromOptions).code
    return createElement(evalCode(mainCode, evalScope))
    }
    ```
    Plumbiu
        9
    Plumbiu  
       2 天前
    又看了一眼,单纯 html 可以用 innerHTML ,安全点用 iframe 做。

    如果有 css 样式隔离,可以用 iframe 喝 ShadowRoot 。React 的 ShadowRoot 可以用这段代码 https://github.com/Plumbiu/blog/blob/main/src/components/function/Shadow.tsx
    importmeta
        10
    importmeta  
       2 天前   ❤️ 1
    楼上的都不是前端, VS Code 安这个 Live Server 插件就完事.
    importmeta
        11
    importmeta  
       2 天前
    @importmeta 我审题错误了... 嵌入 codepen codesanbox 都行
    pikko
        12
    pikko  
       2 天前
    你要的就是漏上的 codepen / codesanbox
    xe2vforesu
        13
    xe2vforesu  
       2 天前
    iframe 就能实现,试试我这个
    https://www.easydo.top/apps/webNew
    gsw945
        14
    gsw945  
       2 天前 via Android
    UnluckyNinja
        15
    UnluckyNinja  
       1 天前
    尽可能简单那就是 codepen/jsfiddle 嵌入,最简单最省事。
    siweipancc
        16
    siweipancc  
       1 天前 via iPhone
    娱乐圈……
    bapp
        17
    bapp  
       1 天前 via Android
    data:text/html,<body oninput="i.srcdoc=h.value+'<style>'+c.value+'</style><script>'+j.value+'</script>'"><style>textarea,iframe{width:100%;height:50%}body{margin:0}textarea{width:33.33%;font-size:18}</style><textarea placeholder=HTML id=h></textarea><textarea placeholder=CSS id=c></textarea><textarea placeholder=JS id=j></textarea><iframe id=i>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1083 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 23:20 · PVG 07:20 · LAX 16:20 · JFK 19:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.