所以我终于找到了一种方法!
您可以使用以下方式混合使用 Telerik 剥离:StripFormattingOptions="Css, Font, Span, ConvertWordLists"
并在 HTML 上使用一些 javascript 粘贴 OnClientPasteHtml="onClientPasteHtml"。
这是删除不需要的标签的代码
function onClientPasteHtml(editor, args) {
var commandName = args.get_commandName();
var value = args.get_value();
if (commandName == "Paste") {
//create a div, set the html content to it,
// remove style attribute, remove non pertinent tags.
var div = document.createElement("DIV");
Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, value);
var nostyle = $(div)[0];
// replace unwanted tags
var strippedHtml = $(nostyle).html().replace(/<\/?([a-z]+)[^>]*>/gi, function (match, tag) {
// any of these is valid
tag = tag.toLowerCase();
return (tag === "em" || tag === "ol" ||
tag === "sub" || tag === "sup" || tag === "ul" || tag === "li"|| tag === "br" || tag === "i") ? match : "";
});
// attributes gone
Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, strippedHtml);
removeAttributes($(div)[0]);
strippedHtml = $(div).html();
args.set_value(strippedHtml);
}
}
function removeAttributes(el) {
while (el.attributes.length > 0) {
el.removeAttribute(el.attributes[0].name);
}
if (el.childNodes.length > 0) {
for (var child in el.childNodes) {
if (el.childNodes[child].nodeType == 1)
removeAttributes(el.childNodes[child]);
}
}
}
我们还添加了一个基于 dtd XHTML 验证的验证器,它将告诉我们复制剪切是否没有忘记任何标记(例如列表的开始和结束标记)。为此,我们使用了 xhtml1-transitional.dtd :
protected bool ValidateMaisRespProjectText()
{
string html = txtJobMainResp.GetHtml(EditorStripHtmlOptions.None);
XHtmlErrors = new List<string>();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
// Create a local reference for validation11.
string xhtmlDtdFile = HttpContext.Current.Server.MapPath("DTD/xhtml1-transitional.dtd");
string newDoctype = string.Format("<!DOCTYPE html SYSTEM \"file://{0}\">", xhtmlDtdFile);
// add doc type validation + root element to make it valid.
html = newDoctype + Environment.NewLine + "<html><head><title>title</title></head><body><div>" + html + "</div></body></html>";
XmlReader reader = XmlReader.Create(new System.IO.StringReader(html), settings);
try
{
while (reader.Read())
{
}
}
catch (Exception ex)
{
XHtmlErrors.Add(ex.Message);
txtJobMainRespValidator1.IsValid = false;
txtJobMainRespValidator1.Text = "* The information entered has invalid HTML content. (DEBUG: " + XHtmlErrors.FirstOrDefault() + ")";
}
finally
{
if (reader != null)
reader.Close();
}
if (XHtmlErrors.Count != 0)
{
txtJobMainRespValidator1.IsValid = false;
txtJobMainRespValidator1.Text = "* The information entered has invalid HTML content. (DEBUG: " + XHtmlErrors.FirstOrDefault() + ")";
}
return XHtmlErrors.Count == 0;
}
private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
XHtmlErrors.Add(string.Format("({0}) {1} - [Line: {2}, Char: {3}]", e.Severity, e.Message, e.Exception.LineNumber, e.Exception.LinePosition));
}
验证会将格式错误添加到列表中。然后你只需要在你的页面中显示你想要的消息。
希望这会有所帮助(如果您发现该解决方案的一些改进,请不要犹豫告诉我:))