简体   繁体   English

我怎么做 <div> 进入 <textarea> 单击“输入键盘按钮”时是否也要复制换行符?

[英]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('&lt;').join('<')
  .split('&gt;').join('>')
  .split('&nbsp;').join(' ')
  .split('&amp;').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.

相关问题 如何使用br中断线制作(复制到剪贴板) - How to make (Copy to clipboard )with br break line 在html textarea中按Enter时如何使用jquery添加换行符 - How would I use jquery to add a line break when pressing enter in a html textarea 当客户端通过按下后退按钮进入页面时,如何使其加载新副本而不是缓存副本? - When the client gets to a page by hitting the back button, how to make it load a fresh copy instead of cached one? 如何通过按键盘上的 Enter 按钮使我的 Google Maps api v3 地址搜索栏工作? - How can I make my Google Maps api v3 address search bar work by hitting the enter button on the keyboard? jQuery:在按Enter键时在光标位置添加换行符 - jQuery: add line break at cursor position when hitting Enter 我该如何复制` <div contentEditable=“true”> 从到 <textarea> `没有剥离出来吗? - How do I copy `<div contentEditable=“true”>` to `<textarea>` without stripping it out? 在中按ENTER <textarea> 打破界限 - Pressing ENTER in <textarea> to break a line 我想用shift + enter在textarea中换行 - I want to break line in textarea with shift+enter 如何在textarea中的enterkey上换行 - How to make the line break on enterkey in textarea 在 contenteditable div 中按回车键时如何添加新 div - How to add new div when hitting enter key in contenteditable div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM