How to hide HtmlElement in webbrowser control

自作多情 提交于 2019-12-21 02:37:28

问题


I have a webbbrowser control that navigates to a page that contains an image, and i want to hide or delete this image from my webbrowser.
I've tried to set on DocumentCompleted event the method below with no luck:

webBrowser1.Document.GetElementById("imgToHide").Style = "display:none";

How to hide an htmlelement from a webbrowser control?

My programing language is C#.

Below is my code:

 private void Form_Load(object sender, EventArgs e)
    {
        webBrowser1.ScriptErrorsSuppressed = true;
        webBrowser1.Navigate(oURL);  
    }
 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        //imgCancel is the name of t he image to hide
        webBrowser1.Document.GetElementById("imgCancel").Style = "display:none";
    }

回答1:


Try making an html file that contain the script :

function SetHidden()
  {
   window.document.all["hiddenText"].style.display="block";
   return "ok";
  }

After that place in your C# code :

Results = (string)WebBrowser1.Document.InvokeScript("SetHidden");

MessageBox.Show(Results);



回答2:


I think i am late.but it may help somebody who need it. You can change the outerhtml of that specific image at run time.Like that

myDoc = this.webBrowser1.Document;

myDoc.GetElementById("imgToHide").OuterHtml = "Changed html here!";



回答3:


You can use runtimeStyle

IHTMLElement2 domElement = element.DomElement as IHTMLElement2;
domInspectBox.runtimeStyle.visibility ="hidden"; // if you want to hide it
domInspectBox.runtimeStyle.display = "none"; // if you want to empty its place


来源:https://stackoverflow.com/questions/2492544/how-to-hide-htmlelement-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!