javascript - 如何从文本框中获取信息并将其移动到段落元素中?

标签 javascript html function onclick submit

对于 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/

相关文章:

javascript - 一个输入字段值不应大于表的其他字段值

javascript - 为什么我不能在 VueJS 的 for 循环中使用变量?

HTML CSS 浏览器侧边栏始终在网站中可见

C 中无法在另一个函数中打印函数的返回值

javascript - 我已经设法找到一种方法来通过使用每个幻灯片来获取我的幻灯片的文本,但是如何让它显示我的 jar 头产品而不是文本?

function - SwiftUI - 将图像从函数返回到 View

javascript - 如何实时判断用户是否选择了文件上传?

javascript - node.js 和浏览器 javascript 中的通用模块

html - 是否有关于哪个 block 元素应该创建边距的约定?

javascript - 使用 JQuery 隐藏和显示多个 div 以及隐藏标题