【问题标题】:How to remove specific style tag in html using C#如何使用 C# 删除 html 中的特定样式标签
【发布时间】:2013-04-22 13:41:51
【问题描述】:

这是我的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>

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

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

应该看起来

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

请帮助...!

【问题讨论】:

标签: c# html css


【解决方案1】:

使用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;

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 来自问题:...using c#
    【解决方案3】:

    $('#tagId').removeAttr('style');

    【讨论】:

    • 这将删除所有样式,而不仅仅是“背景”样式。
    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2017-07-23
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多