我試圖從DIV元素的style屬性中刪除樣式定義。 HTML代碼:
<div class="el1" style="width:800px; max-width:100%" />
...
<div class="el2" style="width:800px; max-width:100%" />
我需要將這些元素中的一個以上應用於操作。
這是我到目前為止使用的HtmlAgilityPack。
foreach (HtmlNode div in doc.DocumentNode.SelectNodes("//div[@style]"))
{
if (div != null)
{
div.Attributes["style"].Value["max-width"].Remove(); //Remove() does not appear to be a function
}
}
我的思維過程是選擇任何一個樣式屬性。查找最大寬度定義並將其刪除。
有關如何實現這一目標的任何指導?
謝謝馬塞爾指出我正確的方向:
這是適合我的解決方案。
HtmlNodeCollection divs = doc.DocumentNode.SelectNodes("//div[@style]");
if (divs != null)
{
foreach (HtmlNode div in divs)
{
string style = div.Attributes["style"].Value;
string pattern = @"max-width(.*?)(;)";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
string newStyle = regex.Replace(style, String.Empty);
div.Attributes["style"].Value = newStyle;
}
}