<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML 查看器</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
height: 100vh;
}
.editor, .preview {
width: 50%;
height: 100%;
box-sizing: border-box;
}
.editor {
border-right: 1px solid #ccc;
}
textarea {
width: 100%;
height: 100%;
padding: 10px;
box-sizing: border-box;
border: none;
outline: none;
resize: none;
}
.preview {
overflow: auto;
padding: 10px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="editor">
<textarea id="html-editor" placeholder="在这里输入 HTML 代码..."><p>Hello, World!</p></textarea>
</div>
<div class="preview" id="html-preview"></div>
<script>
const editor = document.getElementById('html-editor');
const preview = document.getElementById('html-preview');
// 初始化时显示默认内容
updatePreview();
// 监听输入事件,实时更新预览
editor.addEventListener('input', updatePreview);
function updatePreview() {
const htmlContent = editor.value;
preview.innerHTML = htmlContent;
}
</script>
</body>
</html>
HTML 结构:
textarea
用于编辑 HTML 代码,右边是 div
用于实时预览编辑的 HTML 内容。CSS 样式:
JavaScript 功能:
textarea
的输入事件,实时更新右侧预览区域的内容。updatePreview
函数会将 textarea
中的内容直接插入到预览区域中,实现即时预览效果。这个示例代码展示了如何创建一个简单的 HTML 查看器,用户可以在左侧输入 HTML 代码,右侧会实时显示渲染后的结果。
上一篇:html select
下一篇:html表格
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站