简体   繁体   English

从复制和粘贴图像中删除换行符

[英]Removing newline characters from copy and pasting images

I'm creating a an html/javascript application as a project where I essentially am creating a markup language and converting the text to pictures. 我正在创建一个html / javascript应用程序作为一个项目,在这里我实质上是在创建一种标记语言并将文本转换为图片。 One thing I am doing for this is making it possible to copy and paste the string of pictures into the text area and get the markup version so that I can save it in text format. 为此,我要做的一件事就是可以将图片字符串复制并粘贴到文本区域中并获取标记版本,以便可以将其保存为文本格式。

I accomplish this by setting the alt attribute of my html images to what I want to be copied. 我通过将html图像的alt属性设置为要复制的内容来完成此操作。 Something I'm having trouble with is that when I set it up so that it prints the images "down" instead of to the right, accomplished by creating a sequence of <div><img alt="words" src="picture"></img></div> it interprets it as having newline characters between each individual pictures alt when I copy and paste. 我遇到的问题是,当我对其进行设置时,它以“向下”而不是向右打印图像,这是通过创建一个<div><img alt="words" src="picture"></img></div>序列来完成的<div><img alt="words" src="picture"></img></div>它会将其解释为具有每个单独的图像之间的换行字符alt当我复制和粘贴。

I've tried adding a backspace character but that obviously failed, does anybody have a solution for getting rid of these newline characters? 我尝试添加退格字符,但是显然失败了,有人能解决这些换行符吗?

Here's a picture of the output and what you get when you copy and paste to help make it more clear 这是输出的图片,以及在复制和粘贴以使其更清晰时所得到的结果

在此处输入图片说明 在此处输入图片说明

You can attach an event to your textarea that removes any whitespace (or just newlines if that's preferable) when the value changes: 您可以将一个事件附加到您的文本区域,以在值更改时删除所有空格(如果可以的话,则仅删除换行符):

 var pasteArea = document.getElementById('pasteArea'); pasteArea.addEventListener('input', function () { var value = pasteArea.value; var spaceRemoved = value.replace(/\\s/g, ''); if (value !== spaceRemoved) { pasteArea.value = spaceRemoved; } }); 
 textarea { height: 10em; width: 300px; } 
 <textarea id="pasteArea"></textarea> 

var pasteArea = document.getElementById('pasteArea');

pasteArea.addEventListener('input', function () {
  var value = pasteArea.value;
  var spaceRemoved = value.replace(/\n/g, '');

  if (value !== spaceRemoved) {
    pasteArea.value = spaceRemoved;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM