在 HTML 中,可以通过使用 textarea
元素和 div
元素将用户输入的内容显示在页面上。具体步骤如下:
textarea
元素,并为其添加一些样式,例如边框、高度和宽度等。<style>
textarea {
border: 1px solid black;
width: 300px;
height: 200px;
}
</style><div id="output"></div>const textarea = document.querySelector('textarea');
const output = document.querySelector('div');
textarea.value = 'Hello, World!';
textarea.style.position = 'absolute';
textarea.style.top = '100px';
textarea.style.left = '100px';
const updateOutput = () => {
output.innerHTML = textarea.value;
};
textarea.addEventListener('input', updateOutput);<!DOCTYPE html>
<html>
<head>
<title>Input Text and Display in Div</title>
<style>
textarea {
border: 1px solid black;
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<textarea id="input"></textarea>
<div id="output"></div>
<button onclick="inputText()">Submit</button>
<script>
function inputText() {
const textarea = document.querySelector('#input');
const output = document.querySelector('#output');
textarea.value = 'Hello, World!';
textarea.style.position = 'absolute';
textarea.style.top = '100px';
textarea.style.left = '100px';
const updateOutput = () => {
output.innerHTML = textarea.value;
};
textarea.addEventListener('input', updateOutput);
}
</script>
</body>
</html>div
元素,并为其添加一些样式,以便将输入的内容显示在其中。textarea
元素的 value
属性,并将 textarea
元素的位置设置为 div
元素的位置。textarea
元素和 div
元素放在同一个 HTML 页面中,并添加一个按钮以触发输入操作。这样,当用户点击提交按钮时,就会在 div
中显示输入的内容。
领取专属 10元无门槛券
手把手带您无忧上云