这是一个工作演示:
http://jsfiddle.net/SJR3H/7/
$(document).ready(function(){
$('[contenteditable]').on('paste',function(e) {
e.preventDefault();
var text = (e.originalEvent || e).clipboardData.getData('text/html') || prompt('Paste something..');
var $result = $('
$(this).html($result.html());
// replace all styles except bold and italic
$.each($(this).find("*"),function(idx,val) {
var $item = $(val);
if ($item.length > 0){
var saveStyle = {
'font-weight': $item.css('font-weight'),'font-style': $item.css('font-style')
};
$item.removeAttr('style')
.removeClass()
.css(saveStyle);
}
});
// remove unnecesary tags (if paste from word)
$(this).children('style').remove();
$(this).children('Meta').remove()
$(this).children('link').remove();
});
});
我添加了以下行:
$item.replaceWith(function(){
return $("",{html: $(this).html()});
});
它实际上用跨度代替了所有的html标签。在那里,您可以选择将一些标签放在原始文本(h1,p等)中,根据需要对其进行样式化。