【问题标题】:How to hide HtmlElement in webbrowser control如何在 webbrowser 控件中隐藏 HtmlElement
【发布时间】:2011-01-30 08:33:02
【问题描述】:

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

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

如何从浏览器控件中隐藏 html 元素?

我的编程语言是 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";
    }

【问题讨论】:

  • 应该可以。你能给我们看看这个页面吗?
  • 可以试试 style ="visibility:hidden"

标签: c# browser webbrowser-control


【解决方案1】:

您可以使用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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      尝试制作一个包含脚本的 html 文件:

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

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

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

      【讨论】:

        猜你喜欢
        • 2011-08-23
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 2013-09-30
        • 1970-01-01
        • 2020-05-26
        • 1970-01-01
        • 2011-05-29
        相关资源
        最近更新 更多