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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章