【发布时间】:2021-09-29 21:37:44
【问题描述】:
【问题讨论】:
-
How to Ask 欢迎来到 SO - 提问时请阅读此内容
标签: javascript html forms class input
【问题讨论】:
标签: javascript html forms class input
下次,请发布您已经尝试过的内容。
在 Javascript 中,你可以这样做:
const inputField = document.getElementById(...);
const spanField = document.getElementById(...);
inputField.value = spanField.textContent;
【讨论】:
在向世界各地的开发人员提问之前,请先检查一下。检查How do I ask a good question? 我会建议您在使用它们之前正确学习。你需要展示你做了什么。
然而, 如果需要在输入字段中显示文本,可以使用 PLACEHOLDER。
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<div>
<span class='text'>here is some text</span>
<input type='text' class='input' />
</div>
<script type="text/javascript">
document.querySelector('.input').value = document.querySelector('.text').innerHTML
</script>
</body>
</html>
这可能会立即解决您的问题! 请参阅 JavaScript 方法Here
【讨论】:
如果我有你的问题;
document.querySelector('.input').value = document.querySelector('.text').innerHTML
<div>
<span class='text'>here is some text</span>
<input type='text' class='input' />
</div>
现在将 javascript 放在哪里取决于。
如果您想在窗口加载后立即执行;
document.addEventListener('DOMContentLoaded', () => {
// here goes the javascript
})
如果您想通过单击按钮来执行此操作;
document.getElementById('id_of_button').addEventListener('click', () => {
// here goes the javascript
})
【讨论】: