2

我在我的应用程序中使用 nicedit 作为编辑器,我们网站的用户使用从 MS word 或其他一些来源粘贴数据,因此 ui 中断,所以我们决定使用 jquery 从 html 中删除所有格式。

我所做的是删除了所有内联样式和类,但它正在产生问题,因为它也删除了粗体和斜体,因为我们想要保留它。

是他们使用 jquery 删除除粗体之外的所有样式的任何简单方法。

例子 :

<div style="color:red; font-size:13px; font-weight:bold;">test div </div>

在上面的元素中,我希望它为

<div style="font-weight:bold;">test div </div>
4

4 回答 4

1

我还没有测试它是否有效

 $('div').each(function(){
   weight = $(this).css('font-weight');
   $(this).attr('style','');
   $(this).removeClass();
   $(this).css('font-weight',  weight);
});
于 2013-10-07T07:13:26.483 回答
0

我认为唯一的方法是存储您想要的样式,将它们全部删除,然后重新设置它们。

$('.selector').each(function() {
  var keepStyle, $el;
  $el = $(this);
  keepStyle = {
    font-weight: $el.css('font-weight'),
    font-style : $el.css('font-style')
  };

  $el.removeAttr('style')
    .css(keepStyle);
})
于 2013-10-07T07:13:35.637 回答
0

您可以使用jquery 数据方法将此属性存储为数据 ,并在清除所有样式后恢复

于 2013-10-07T07:11:11.427 回答
0
var $temp = $('<div><div style="color:red; font-size:13px; font-weight:bold;">test div </div></div>');

 $temp.find('*').each(function(){
    var fontWeight = $(this).css('font-weight');
$(this).removeAttr('style');
if(fontWeight!=undefined){
     $(this).attr('style','font-weight:'+fontWeight);
}
});
于 2013-10-07T07:18:31.713 回答