复制富文本内容带换行


   // 拷贝详情文字
    copyToClipboard(stripHtmlKeepNewlines(account.details))

 function stripHtmlKeepNewlines(html){
    // 用特殊占位符替换所有换行标签(需覆盖不同写法的
const withPlaceholders = html .replace(//gi, '\n') // 处理


.replace(/<\/p>/gi, '\n') // 处理段落结束 .replace(/<\/div>/gi, '\n'); // 处理 div 结束 // console.log(withPlaceholders,"s") return scriptHtml(withPlaceholders) } function scriptHtml(html){ var temp = document.createElement("template"); temp.innerHTML = html return temp.content.textContent || "" } export function copyToClipboard(text){ if (navigator.clipboard && window.isSecureContext){ // https navigator.clipboard.writeText(text).then(()=>{ toast("复制成功") }).catch(()=>{ toast("复制失败","error") }) }else { var input = document.createElement("input"); input.setAttribute("value",text) document.body.appendChild(input) input.select() try { document.execCommand("copy") toast("复制成功") } catch (e) { toast("复制失败","error") } document.body.removeChild(input) } }

你可能感兴趣的:(复制富文本内容带换行)