js实现复制并能保留原格式粘贴

使用textarea能粘贴的时候保留原格式。

使用input粘贴的时候格式会丢失。

      const input = document.createElement('textarea');
      document.body.appendChild(input);
      input.value = “复制我呀”;
      // input.setAttribute('value', "复制我呀");
      input.select();
      if (document.execCommand('copy')) {
        document.execCommand('copy');
        console.log('复制成功');
      }
      document.body.removeChild(input);

 

你可能感兴趣的:(前端,js复制粘贴,js粘贴保留原格式)