Skip to main content
 首页 » 编程设计

c#之将 html 元素添加到 webBrowser

2024年02月05日27dudu

我想添加一个"<div> </div>"到我的浏览器页面。

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

请您参考如下方法:

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