quot html复制,javascript - Copy HTML to clipboard - Stack Overflow

本文探讨了在使用第三方库复制编程代码(如HTML和Python)到剪贴板时遇到的问题。当尝试复制包含HTML的字符串时,浏览器误将其视为HTML文档并尝试解析样式,导致错误。问题在于`data-clipboard-text`属性中的内容包含空白字符和换行符。解决方法可能包括正确编码HTML字符串或寻找能够处理HTML片段的库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I use a simple third party library to copy something to clipboard.

The something is always some programming code. In these examples it is HTML and Python codes.

Copy (fail)

Copy (success)

In short: whatever is placed in data-clipboard-text attribute of a button, will be copied to the clipboard.

Please, press a Copy button in the example and insert into a text editor what there is in the clipboard.

Copy (fail) button copies the contents of its data-clipboard-text but it consists only of whitespace characters and line breaks.

Morever, when I run this at my localhost, I get this error after clicking the fail button:

Refused to apply style from 'http://localhost:8000/draft/linux/install-os-ubuntu/stylesheets/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

The reason seems that HTML is considered to be a real HTML. The browser looks for sources, fails and sends the error. But for my purpose this is just a string.

6PUGn.png

Could you help me understand why this situation occurs and cope with the problem of copying HTML documents.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值