对于 javascript 来说,我完全是个新手。我有一个文本框和一个与之关联的按钮。当我运行 onclick 函数时,我想从文本框中获取信息并将其移动到:
“当使用 onclick 函数单击提交按钮时,文本将从文本框移动到此处。
.我尝试了很多不同的方法,但到目前为止还没有成功。 谢谢您的帮助。
干杯!
最佳答案
如果您想使用 jQuery,这里有一个简单的解决方案。
/**
* Using jQuery, grab the <pre> element on the page, and fill
* them with the text within the textarea.
*/
function moveText() {
"use strict";
$("pre").html($("textarea").val());
}
/**
* Using pure javascript, do exactly the same thing as moveText();
*/
function pure_js() {
"use strict"
document.getElementsByTagName("pre")[0].innerHTML = document.getElementsByTagName("textarea")[0].innerHTML;
}
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<textarea>Default text.</textarea>
<button type="button" onClick="pure_js()">Move the Text</button>
<pre></pre>
</body>
</html>
关于javascript - 如何从文本框中获取信息并将其移动到段落元素中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995426/