2013-04-22 74 views
-1

这里删除特定HTML标签的风格是我的html代码:如何使用C#

<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> 

请帮助...!

+3

退房的HTML敏捷性包。 – 2013-04-22 13:42:53

+0

你可以在jQuery中轻松做到这一点,为什么它需要成为C#? – DGibbs 2013-04-22 13:42:53

+0

你如何访问这个HTML? – 2013-04-22 13:43:24

回答

-3

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

+0

这将删除所有样式,而不仅仅是“背景”样式。 – 2013-04-22 14:29:40

-2

试试这个

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

来自问题:'... using c#' – 2017-09-13 15:45:19

2

从使用的NuGet 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; 
+0

This is cool ..! thankx。但我不应该使用这些包:(... – Aruna 2013-04-23 01:44:38