4

我试图剥离粘贴到Telerik RadEditer上的MS Word格式。不幸的是,我似乎无法使内置格式的剥离器工作。如何从每个标签中删除每个属性?

//performing paste 
var editor = $find("radEditor1"); 
editor.setFocus(); 
var rng = editor.getSelection().getRange(); 
rng.execCommand("Paste", null, false); 

//does nothing! (even when uncommented) 
//editor.fire("FormatStripper", {value: "MSWordRemoveAll" }); 

所以,我想我可以杠杆jQuery的字符串所有属性的标签,这可能只是正是我所需要的。

//fixing content 
var html = editor.get_html(); 
$("*", html).each(function(){ 
    var attr = $.map(this.attributes, function(item){ 
     return item.name; 
    }); 
    var node = $(this); 
    $.each(attr, function(i, item){ 
     //added a filter for crazy Error 
     if(item != "dataSrc" && 
      item != "implementation" && 
      item != "dataFld" && 
      item != "dataFormatAs" && 
      item != "nofocusrect" && 
      item != "dateTime" && 
      item != "cite") 
      node.removeAttr(item); 
    }); 
}); 
editor.set_html(html); 

现在,经过这个函数完成我的HTML变量没有它的HTML更新中...

+0

改为尝试'return item.nodeName'。 –

+0

你知道哪条线路出现错误吗? –

+0

nodeName给出相同的错误,第2764行jquery-1.7.2.js –

回答

0

我想我有。

var html = editor.get_html(); 
var parent = $('<div></div>').html(html); 
$(parent).find('*').each(function(){ 
    var attr = $.map(this.attributes, function(item){ 
     return item.name; 
    }); 
    var tag = $(this); 
    $.each(attr, function(i, item){ 
     tag.removeAttr(item); 
    }); 
}); 
editor.set_html(parent.html());​ 

http://jsfiddle.net/duC6Z/8/

不幸的是,我仍然有我的RadEditor问题,但认为是正常的。

1

此代码似乎这样的伎俩。它使用safeAttrs数组来更新要保持更容易的属性列表。您可以传递.removeAttr()空格分隔的属性列表以删除,因此您不需要循环遍历属性名称一个接一个。

最后,不同的浏览器可以处理不同的属性(浏览器,例如,卖场以小写所有的属性,因此“DATAFLD”存储为“DATAFLD”),所以最好正常化他们所有.toLowerCase()

var safeAttrs = ["datasrc","implementation","datafld","dataformatas","nofocusrect","datetime","cite"]; 

$('html *').each(function() { 
    var attrs = $.map(this.attributes,function(attr) { 
     if($.inArray(attr.nodeName.toLowerCase(),safeAttrs) < 0) { 
      return attr.nodeName; 
     } else { return null; } 
    }); 
    $(this).removeAttr(attrs.join(' ')); 
}); 

jsFiddle DEMO。使用Chrome或Firebug检查生成的元素以检查属性是否已被删除。

+0

谢谢杰克,我想我可以将你的安全顾问加入到我的解决方案中。 –