起因:
之前需求有個右擊復制文本的功能,當時出現的情況是復制后文本的換行符無效了,因為當時時間比較趕,我就用了方案一解決的,但是上線后用戶不買單呀,因為剪切板的內容始終是不帶換行符的
解決:
有問題的代碼
1 let inputNode = document.createElement('input') 2 if (contextmenuChatRecord.msgType === MESSAGE_SUB_TYPE.VMA_TEXT_ELEM) { 3 // inputNode.value = JSON.stringify(contextmenuChatRecord.content) 4 inputNode.value = contextmenuChatRecord.content 5 this.$emitBus('copyTextMsg', contextmenuChatRecord.content) 6 } else { 7 inputNode.value = JSON.stringify(contextmenuChatRecord) 8 } 9 let copyContentDom = this.$el.querySelector('#copy-content') 10 copyContentDom.appendChild(inputNode) 11 inputNode.select() 12 document.execCommand('copy') 13 copyContentDom.removeChild(inputNode) 14 inputNode = null
這里要穿插一下 這種寫法肯定是沒問題的 問題出在HTML標簽上 HTML的input標簽是不支持換行的所以問題就卡在這
當然解決也很簡單 createElement('input') 修改成 document.createElement('textarea')