如何使用 Javascript 将文本从文本区域移动到新表中
How would I move text from text areas into a new table using Javascript?
我只是在学习Javascript,我们的老师没有资格教他一生中从未使用过任何形式的代码。我想要一个 onClick 事件来触发一个函数,该函数将专用表中 3 个文本框的内容移动到用于打印的新表格上的特定位置。
这是具有三个文本区域的表的代码:
<table>
<tr>
<td rowspan="2">
<textarea rows="19" cols="52"> Notes directly from the source should go in this box. (copy and paste)</textarea>
</td>
<td> <textarea rows="3" cols="30">The URL or web-address of the source should go here.</textarea>
</td>
</tr>
<tr>
<td> <textarea rows="15" cols="30">Additional notes should go in this box.</textarea>
</td>
</tr>
</table>
这个想法是将 3 段文本移动到数字记事卡上。URL 在顶部,直接注释在中间,学生的笔记在底部。所有的基本格式略有不同。使用一个onClick函数是否可以做到这一点,或者使用三个功能会容易得多。请记住,如果我确实使用了三个函数,我需要它们都由同一事件触发。
您可以使用 jQuery 并将内容从一个 HTML 节点复制/粘贴/删除到另一个 HTML 节点中:
$('#idOfYourTargetNode').text($('#idOfYourFirstNode').text());
$('#idOfYourFirstNode').empty();
只需给相应的元素一个喜欢:
<td id="url">Content url ....</td>
而你的目标像这样:
<h1 id="headlineUrl"> URL Headline </h1>
您可以在脚本标记中执行此操作:
$('#headlineUrl').text($('#url').text());
$('#url').empty();
触发它单击您的元素。不要忘记包含jQuery。
首先,如果文本区域具有可以引用的 id,这将是最简单的:
<tr>
<td rowspan="2">
<textarea id="notes" rows="19" cols="52"> Notes directly from the source should go in this box. (copy and paste)</textarea>
</td>
<td> <textarea id="address" rows="3" cols="30">The URL or web-address of the source should go here.</textarea>
</td>
其他注释应在此框中。
然后,您可以执行以下操作:
$("button").on("click", function() {
$("#newlocation1").text($("#notes").text());
$("#newlocation2").text($("#address").text());
$("#newlocation3").text($("#additional").text());
}
虽然,从这一点开始,您可以通过多种方式使用这些文本。
解决方案
var getValueBySelector = function(c){
return document.querySelector(c).value;
};
var each = function(array, cb){
for(var i=0; i<array.length; i++) cb.call(array[i], i, array[i]);
};
var concatenate = function(array, sep){
var concat = [];
each(array, function(){
concat.push(getValueBySelector(this));
});
return concat.join(sep);
};
document.querySelector('.concat').innerHTML = concatenate(['.notes', '.url', '.add'], '<br>');
<table>
<tr>
<td rowspan="2">
<textarea class="notes" rows="19" cols="52"> Notes directly from the source should go in this box. (copy and paste)</textarea>
</td>
<td> <textarea class="url" rows="3" cols="30">The URL or web-address of the source should go here.</textarea>
</td>
</tr>
<tr>
<td> <textarea class="add" rows="15" cols="30">Additional notes should go in this box.</textarea>
</td>
</tr>
</table>
<div class="concat"></div>
相关文章:
- 在不移动内部文本的情况下缩放元素的效果
- 在下拉导航中将文本向左移动
- 如何在悬停时显示文本而不移动页面上的内容
- 当文本不断变化时,如何避免在按钮内移动文本
- 在移动浏览器上显示大型文本文件
- 替换移动设备的链接文本
- 移动HTML输入文本框在提交时隐藏软键盘
- Javascript将选定的web文本移动到MS Word
- 为什么我的导航按钮或按钮文本不会移动到中心
- 如何在单击下一步时使文本移动/更改
- 调整父元素大小时,文本会移动
- 如何将两个段落连接在一起以内联方式显示,而不会使文本移动
- 同时调整窗口导航栏文本移动的大小
- CSS中的文本移动动画
- 防止d3缩放将SVG中的文本移动到与SVG其余部分不同的位置
- 将表中的文本移动到最近的文本输入中
- 我如何使我的文本移动动画在画布上更平滑(少锯齿运动)
- 禁用javascript文本移动
- 将总和文本移动到图表序列图中的最右侧
- 如何使文本移动到 JQuery 幻灯片下方