您的位置:首页 > 科技 > IT业 > js 复制文本带样式

js 复制文本带样式

2024/7/6 1:17:06 来源:https://blog.csdn.net/Meetacoo/article/details/140093484  浏览:    关键词:js 复制文本带样式

一键复制带样式的html文本到邮件

<div><div id='copy-content'><div style={{ fontSize: '16px',fontWeight: 500, lineHeight: '24px', color: '#222', marginBottom: '16px' }}>邀请您参加腾讯会议网络研讨会(Webinar)</div></div><Button onClick={handleCopy}>Copy</Button>
</div>
  1. 不带样式的纯文本
const handleCopy = () => {// 获取目标元素的innerHTMLlet innerHtml = document.getElementById('copy-content').innerHTML || ''// 处理innerHTML,使用转义符代替<br>标签const html = innerHtml.replace(/<br>/g, '\n').replace(/<[^>]*>/g, '');// 这是比较新的API,是一个异步方法navigator.clipboard.writeText(html).then(() => {// 按需处理想要的callbackmessage.success('复制成功');});}
  1. html文本
const handleCopy = () => {// 获取目标元素的innerHTMLlet innerHtml = document.getElementById('copy-content').innerHTML || ''// 处理innerHTML,使用转义符代替<br>标签const html = innerHtml.replace(/<br>/g, '\n');// 这是比较新的API,是一个异步方法navigator.clipboard.writeText(html).then(() => {// 按需处理想要的callbackmessage.success('复制成功');});}
  1. 带样式的纯文本
  const handleCopy= () => {const range = document.createRange();const element = document.getElementById('copy-content'); // 要复制的元素range.selectNode(element);window.getSelection().removeAllRanges();window.getSelection().addRange(range);document.execCommand('copy');window.getSelection().removeAllRanges();message.success('复制成功');};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com