使用 CSS 或 Javascript 复制/剪切时从文本中删除样式

2023-10-02前端开发问题
65

本文介绍了使用 CSS 或 Javascript 复制/剪切时从文本中删除样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

哟,

好吧,一直在研究这个:如何在不带任何样式包袱(背景颜色、颜色等)的情况下复制/剪切样式文本?

Alright been noodling on this one for a while: How copy/cut styled text without bringing along any style baggage (background-color, color, etc)?

已被挫败的几条攻击路线:

Couple of routes of attacks that have been foiled:

  1. 使用 ::select 为文本设置不同的样式? 不起作用, ::style 没有被复制
  2. 使用 jQuery 的选择绑定设置所选文本的样式 这仅适用于输入,而不适用于 p、div
  3. 通过使用 jQuery 将事件绑定到复制/粘贴来拦截和删除样式? 无法访问复制的对象以删除内容,尝试使用 e.preventDefault();然后返回事件对象,但这也不起作用
  4. 保存后修改剪贴板数据? 同样没有骰子,大多数浏览器不会让你在没有 Flash 和某种确认的情况下进入这个

无论如何,想法?似乎它对于具有白色背景颜色的网站非常有用.

Anyway, thoughts? Seems like it would be very useful for sites that have white background colors.

推荐答案

我现在没有时间编写示例,但是您可以为键盘快捷键触发的剪切/复制执行此操作.它不适用于通过上下文菜单或编辑菜单选项进行剪切/复制,因为它依赖于在剪切或复制事件触发之前更改用户选择.

I haven't got time to code up an example now, but you could do this for cut/copy triggered by keyboard shortcuts. It wouldn't work for cut/copy via context menu or Edit menu options because it relies on changing the user selection before the cut or copy event fires.

步骤:

  1. 处理 Ctrl-CCtrl-X 键盘快捷键和 Mac 等效项.
  2. 在此处理程序中,创建一个离屏元素(例如,绝对位置和左侧 -10000 像素)并将所选内容复制到其中.您可以使用 window.getSelection().getRangeAt(0).cloneContents() 来执行此操作,尽管您需要单独的 IE 代码 <9 并且您应该检查选择是否折叠.
  3. 随心所欲地更改屏幕外元素内容的样式.
  4. 移动选择以包含屏幕外元素的内容,以便剪切或复制此内容.
  5. 使用 window.setTimeout() 添加一个短暂的延迟(几毫秒),该延迟会调用一个删除屏幕外元素并恢复原始选择的函数.
  1. Handle the Ctrl-C and Ctrl-X keyboard shortcuts and the Mac equivalents.
  2. In this handler, create an off-screen element (absolute position and left -10000px, say) and copy the selected content into it. You can do this using window.getSelection().getRangeAt(0).cloneContents(), although you'll need separate code for IE < 9 and you should check the selection is not collapsed.
  3. Do whatever you like to to change the styling of the content of the off-screen element.
  4. Move the selection to encompass the content of the off-screen element so that it is this content that is cut or copied.
  5. Add a brief delay (a few milliseconds) using to window.setTimeout() that calls a function that removes the offscreen element and restores the original selection.

这篇关于使用 CSS 或 Javascript 复制/剪切时从文本中删除样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125