如何避免WebKit contentEditable复制粘贴导致不需要的CSS? [英] How to avoid WebKit contentEditable copy-paste resulting in unwanted CSS?

查看:1579
本文介绍了如何避免WebKit contentEditable复制粘贴导致不需要的CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< div> contentEditable =true

当我在可编辑的div 中复制粘贴内容时,粘贴的文本会包含很多不需要的CSS。

When I copy-paste content within the editable div, the pasted text gets wrapped into a lot of unwanted CSS.

例如,
< p>文本文本< / p>
变为:

For example, this: <p>text text</p> becomes:

<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; outline-width: 0px; outline-style: initial; outline-color: initial; font-size: 17px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; background-position: initial initial; background-repeat: initial initial; ">text text</p>

我理解浏览器(目前的情况下是Google Chrome)尝试智能化,我最终是完全不相干和不需要的CSS。

I understand the browser (Google Chrome in the current case) tries to be smart and all, but what I end up is completely irrelevant and unwanted CSS.

有没有办法告诉基于WebKit的浏览器 p>

Is there any way to tell the WebKit-based browser not to generate this?

推荐答案

我有同样的问题,加上每个浏览器创建不同HTML的问题。所以我写了一个JavaScript端口的清理库: Sanitize.js

I had the same problem, plus the problem that every browser creates different HTML. So I wrote a JavaScript port of the Sanitize library: Sanitize.js

Sanitize.js是一个基于白名单的HTML清理器。给定一个可接受的元素和属性列表,Sanitize.js将从DOM节点中删除所有不可接受的HTML。

Sanitize.js is a whitelist-based HTML sanitizer written in JavaScript. Given a list of acceptable elements and attributes, Sanitize.js will remove all unacceptable HTML from a DOM node.

查看示例,其中捕获粘贴事件,然后处理HTML。

Have a look at the example, where I capture the paste event and process the HTML afterwards.

这篇关于如何避免WebKit contentEditable复制粘贴导致不需要的CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆