代码之家  ›  专栏  ›  技术社区  ›  Gaby

如何在webbrowser控件中隐藏HtmleElement

  •  4
  • Gaby  · 技术社区  · 16 年前

    我有一个浏览器控件,可以导航到包含图像的页面, 我想从我的网络浏览器中隐藏或删除此图像。
    我尝试过使用下面的方法,但没有成功:

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

    如何对webbrowser控件隐藏HtmleElement?

    我的编程语言是C#。

    下面是我的代码:

     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";
        }
    
    3 回复  |  直到 13 年前
        1
  •  1
  •   Viktor Apoyan    15 年前

    尝试创建包含脚本的html文件:

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

    在你的C#代码中的那个位置之后:

    Results = (string)WebBrowser1.Document.InvokeScript("SetHidden");
    
    MessageBox.Show(Results);
    
        2
  •  1
  •   Zain Ali    15 年前

    我想我迟到了。但它可能会帮助需要它的人。 您可以在运行时更改该特定映像的outerhtml。像那样

    myDoc = this.webBrowser1.Document;
    
    myDoc.GetElementById("imgToHide").OuterHtml = "Changed html here!";
    
        3
  •  1
  •   Ahmad    11 年前

    你可以用 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