如何从Windows窗体webbrowser控件中删除元素

时间:2015-10-18 09:37:59

标签: c# html forms

我想知道一种在Windows窗体的Web浏览器中删除网站元素的方法。我有这段代码:

namespace Browser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.GetElementById("ads").Style = "display:none";
            webBrowser1.Document.GetElementById("navigation").Style = "display:none";
            webBrowser1.Document.GetElementById("donate").Style = "display:none";
            webBrowser1.Document.GetElementById("social_bookmarking_buttons").Style = "display:none";
        }
    }
}

正如您可能注意到的那样,我只是隐藏了元素,我想删除它们。谢谢你的时间。

1 个答案:

答案 0 :(得分:2)

这对我来说可以删除元素。

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.GetElementById("ads").OuterHtml = "";
    webBrowser1.Document.GetElementById("navigation").OuterHtml = "";
    webBrowser1.Document.GetElementById("donate").OuterHtml = "";
    webBrowser1.Document.GetElementById("social_bookmarking_buttons").OuterHtml = "";
}