我想使用文本区域在 HTML 代码和预览 html 之间切换
当点击 HTML 时,它会显示隐藏文本区域的代码 当再次点击 HTML 时,它会再次显示
我想要设计这样的东西
我尝试过
<style>
.container {
    width:500px;
    position: fixed;
}
.right-element {
    background: red;
    display: inline-block;
    position: absolute;
    right: 0;
}
</style>
<div class="container">
    <div class="right-element">
        Preview
    </div>
  <textarea id="w3review" name="w3review" style="position:relative;width:100%;height:75%;resize: none; " ><h3>At w3schools.com you will learn how to make a website. They offer free tutorials in all web development technologies.</h3></textarea>    
</div>
            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以有另一个
div专门用于显示预览。然后,当用户切换 HTML 视图时,将文本区域的值插入到div的innerHTML中并显示它。但这可能会让您的应用程序遭受 XSS 攻击,因此使用时要小心。
$('.right-element').click(function() { $(this).toggle() $(this).siblings().toggle() togglePreview() }) let showPreview = false const w3Preview = $('#w3review-preview') function togglePreview() { if (!showPreview) { w3Preview.html(w3review.value) w3Preview.show() $(w3review).hide() } else { w3Preview.hide() $(w3review).show() } showPreview = !showPreview }#html,#w3review-preview{display:none}.container{position:fixed;width:500px}.right-element{background:red;display:inline-block;position:absolute;right:0;z-index:1}