如何使用C#删除html中的特定样式标记

时间:2013-04-22 13:41:51

标签: c# html css

这是我的HTML代码:

<p><span style="background:lime;Color:Red;">Contrary to popular belief, <b><u>Lorem Ipsum is not simply</u></b> random text. It has roots in a piece of classical Latin literature from <span style="background:blue;">45 BC, making it over 2000 years</span> old. Richard McClintock, </span><b>

从上面的代码中,我需要删除背景属性&amp;使用所有跨度中的C#的值。样式标记中的其他值应保留。例如:

<span style="background:lime;Color:Red;">Contrary to popular belief,.....</span>

应该看

<span style="Color:Red;">Contrary to popular belief,.....</span>

请帮忙......!

3 个答案:

答案 0 :(得分:2)

使用HtmlAgilityPack

string html = @"<span style=""background:lime;Color:Red;"">Contrary to popular belief,.....</span>";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

foreach (var span in doc.DocumentNode.Descendants("span"))
{
    var style = span.Attributes["style"].Value;
    span.Attributes["style"].Value = String.Join(";", style.Split(';').Where(s => !s.ToLower().Trim().StartsWith("background:")));

}

var newHtml = doc.DocumentNode.InnerHtml;

答案 1 :(得分:-2)

试试这个

$('span').css("background", "")

答案 2 :(得分:-3)

$( '#TAGID')removeAttr( '风格');