C# 如何将内联样式注入正在加载到WPF WebBrowser中的文档?

C# 如何将内联样式注入正在加载到WPF WebBrowser中的文档?,c#,html,css,wpf,mshtml,C#,Html,Css,Wpf,Mshtml,我一直在尝试使用mshtml修改第三方web api。现在我正在尝试将两个元素的“显示”属性更改为“无”,以便它们不可见 我知道他们的身份证 第一个是img,id是zendbox\u close。第二个是div,id是zenbox\u scrim html如下所示 <div class="zenbox_header"> <img id="zenbox_close"></img> </div> ... <div id="zenbox_s

我一直在尝试使用mshtml修改第三方web api。现在我正在尝试将两个元素的“显示”属性更改为“无”,以便它们不可见

我知道他们的身份证

第一个是
img
,id是
zendbox\u close
。第二个是
div
,id是
zenbox\u scrim

html如下所示

<div class="zenbox_header">
    <img id="zenbox_close"></img>
</div>
...
<div id="zenbox_scrim...></div>
<div class="zenbox_header">
    <img id="zenbox_close" style="display:none;"></img>
</div>
...
<div id="zenbox_scrim style="display:none;"...></div>
我在另一篇帖子中看到有人在谈论注入脚本,他们说你可以使用

IHTMLElement scriptEl = doc.CreateElement("script");
不过,我不确定与此类似的HTML元素是什么。我还必须使用
IHTMLDocument3
来使用方法
getElementById
,但该类似乎不包含任何类似于
CreateElement()
的内容


我的问题是如何将内联样式插入正在加载到我的
WPF
WebBrowser
中的
文档中?

是的,您可以内联操作样式

这样做的一个好方法是在使用IHTMlement时使用IHTMLStyle或IHTMLCurrentStyle接口。这两者反映的值存在一些差异,它们并不总是同步的。更好地解释原因是:

代码示例如下所示:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        wb.LoadCompleted += wb_LoadCompleted;
        wb.Source = new Uri("http://www.google.com");          
    }

    void wb_LoadCompleted(object sender, NavigationEventArgs e)
    {
        var doc = wb.Document as HTMLDocument;
        var collection = doc.getElementsByTagName("input");

        foreach (IHTMLElement input in collection)
        {
            dynamic currentStyle = (input as IHTMLElement2).currentStyle.getAttribute("backgroundColor");

            input.style.setAttribute("backgroundColor", "red");                
        }



    }
}
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        wb.LoadCompleted += wb_LoadCompleted;
        wb.Source = new Uri("http://www.google.com");          
    }

    void wb_LoadCompleted(object sender, NavigationEventArgs e)
    {
        var doc = wb.Document as HTMLDocument;
        var collection = doc.getElementsByTagName("input");

        foreach (IHTMLElement input in collection)
        {
            dynamic currentStyle = (input as IHTMLElement2).currentStyle.getAttribute("backgroundColor");

            input.style.setAttribute("backgroundColor", "red");                
        }



    }
}