javascript - 如何使用 JavaScript 或 jQuery 将一些文本插入到 textarea 中?
问题描述
我有两个文本区域 - 一个用于在其中粘贴一些文本,另一个用于在双击它们后从第一个文本区域插入单词。我怎样才能让它发生?
我已经在以下情况下取得了一些结果: 1.将一些文本粘贴到 textarea 2.双击 textarea 中的单词 3.查看该单词如何出现在带有 ul 的 div 中。字加为里。见案例代码:
//html block
<textarea name="" id="text" cols="30" rows="10" ondblclick="copyPaste()" >Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur minus iure suscipit quam expedita? Sed minus laboriosam natus quaerat autem enim accusantium, architecto officiis aliquam pariatur. Adipisci provident tenetur velit!</textarea>
<div id="wordList" class="wordListclass">
<ul id="myList">
<li></li>
</ul>
</div>
</body>
//js block for copy-pasting words after doubleclick on the text from the texarea with id ='text'
"use strict";
function copyPaste(){
var selection = window.getSelection();
console.log(selection.toString());
var node = document.createElement("LI");
var selectionWithButton = selection;
var textnode = document.createTextNode(selectionWithButton);
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
现在我需要摆脱并添加第二个文本区域。我想看看双击第一个文本区域中的文本后单词如何出现在第二个文本区域中。重要说明 - 它们应具有以下结构:
单词1
单词
2 单词3
没有 html 标签,因为在第二个文本区域中看到这些单词的列表后,我想将它们插入到数据库中,因此 html 标签(如我提供的代码中所示)将是不可取的。不幸的是,用 textarea 替换 div 元素不起作用。感谢大家的阅读和帮助!
解决方案
const myList = document.querySelector("div#wordList ul#myList") // Get the list
function copyPaste(){
let textAreaValue = document.querySelector("textarea#text").value //get the written text in textarea
myList.innerHTML += `<li> ${textAreaValue} </li>` //put the "textAreaValue" in the list
}
像这样的东西?
推荐阅读
- python - 外壳排序显示“列表索引超出范围”,我无法弄清楚
- laravel - 启动 Vue.js 变量作为服务器生成的值
- python - 困境试图将此代码与以下代码绑定以制作密码进入程序
- python - 多处理未在 python 3 中正确执行函数
- amazon-web-services - AWS CLI 查找包含允许来自所有来源的所有流量的规则的所有安全组
- python - Beautiful Soup and Splinter - 获取 href 和 src 属性
- postgresql - 在 Liquibase 中,如何表示使用公用表表达式 (CTE) 的 postgres 视图?
- laravel - 如何解决这个问题,盯着我的 laravel 项目安装 npm
- javascript - 原生基地用一条线
- javascript - 如何在Angular 7的数字类型输入字段中添加/限制2个小数.00