首页 > 解决方案 > 使从绝对定位的 div 复制的文本保留空格

问题描述

我正在实现 OCR,我需要在图像顶部呈现已识别的文本。我使用绝对定位的 div。每个词的每个 div。当我选择一个句子并复制它时购买,所有的空格都消失了。

我怎样才能让它用空格复制?

const words = ["lorem", "ipsum", "dolor", "sit", "amet"];

const container = document.getElementById("words");

let i = 0;
for (const w of words) {
  const el = document.createElement("div");
  el.innerText = w;
  el.style.width = 30 + "px";
  el.style.height = 10 + 'px';
  el.style.top = (i * 10) + "px";
  el.style.left = (i * 40) + "px";
  i += 1;
  
  container.appendChild(el);
  
  const space = document.createElement("span");
  space.innerHtml=" ";
  el.appendChild(space);
}

这是一个例子:https ://codepen.io/konstantin-edward-alikhanov/pen/YzpeNwV?editors=0110

标签: javascripthtmlcss

解决方案


将反引号与ES6 模板文字一起使用。

el.innerText = w + ` `;

推荐阅读