[英]How do I make <div> into <textarea> copy to copy the line break too when hitting “enter keyboard button”?
How do I make when copying to to copy the line break too when i push enter keyboard button so that it is an exact copy? 当我按下Enter键时,如何复制到复制行中断处也要如何做,以便进行精确复制? Right now it works, but it does not copy line break when pushing the enter keyboard button.
现在可以使用,但是在按Enter键盘按钮时不会复制换行符。 And I want it to work as it does now without having it stripped.
我希望它能够像现在一样工作而不会被剥离。
NOTE because of former misunderstanding I clarify it: Please do not confuse "copying texatea into div" because that is not the same thing as "coping div into textarea", because the code working for "copying texatea into div" does not work the same as "coping div into textarea". 注意:由于以前的误解,我将其澄清:请不要混淆“将texatea复制到div”,因为这与“将divate复制到textarea”不同,因为“将texatea复制到div”的代码无法正常工作作为“将div划分为textarea”。 I need to "coping div into textarea" and not the other way around.
我需要“将div匹配到textarea中”,而不是相反。
I want: div into text 我想要:div到文本
NOT: text into div 不:将文本插入div
JavaScript: JavaScript:
function copyText() {
$("#text").val($("#divtext").text())
}
html: 的HTML:
<textarea name="text" id="text" rows="14" cols="54" wrap="soft"></textarea>
You can look for the enter keydown also: 您还可以查找Enter keydown:
<script> function onTextChange() { var key = window.event.keyCode; // If the user has pressed enter if (key === 13) { document.getElementById("text").value = document.getElementById("text").value + "\\n*"; return false; } else { return true; } } </script>
<div id="yourDiv" onkeypress="onTextChange();"></div>
It is hard to get line breaks from div
if you use .text()
, try using .html()
如果您使用
.text()
,则很难从div
获得换行符,请尝试使用.html()
Split <br>
from div
and join using \\n
for textarea
从
div
拆分<br>
并使用\\n
进行textarea
$('#text').val($('#divtext').html().split('<br>').join('\\n'))
WORKING UPDATE 工作更新
I used the code from an answer I got, but I had to modify it a bit to make it work properly because the answer I got was incomplete. 我使用了获得的答案中的代码,但由于获得的答案不完整,因此必须对其进行一些修改以使其正常工作。 Because I want the code copied but not stripped out, then what was needed to do is to let it strip out as default and then strip it in again so it shows as source codes when typing source codes, and not as a striped out text.
因为我要复制代码而不是剥离代码,所以需要做的是将其剥离为默认值,然后再次剥离,以便在键入源代码时显示为源代码,而不是剥离文本。
So here is the complete answer that worked and how I made it work out of the incomplete answer I got. 因此,这是行之有效的完整答案,以及如何从我得到的不完整答案中使它起作用。 This code works.
此代码有效。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
function copyText() {
$('#text').val($('#divtext').html()
.split('<br>').join('')
.split('<div>').join('\n')
.split('</div>').join('')
.split('<').join('<')
.split('>').join('>')
.split(' ').join(' ')
.split('&').join('&')
)
}
</script>
<div id="divtext" contentEditable="true" wrap="soft" style="text-align:left; width: 499px; height: 230px;" onkeyup="copyText();"></div>
<br>
<textarea id="text" rows="34" cols="134" wrap="soft"></textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.