将html元素添加到webBrowser

时间:2015-10-04 10:53:32

标签: c# html winforms

我想在我的webBrowser页面添加"<div> </div>"

我已导航到http://google.com,例如我想在该页面上添加我自己的div,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:4)

使用WebBrowser控件:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate("http://www.google.com");
        this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        string content = "<div style=\"background:#f00;\"><h1>HACKED</h1></div>";
        this.webBrowser1.Document.GetElementById("gb").InnerHtml = content;
    }
}

enter image description here