2

是的,我对 webBrowser 控件的使用在 IE8 中可以正常工作,而在 IE9 中则不行。似乎将 HTMLDocument 从 DesignMode = "On" 设置为 DesignMode = "Off" 会从 WebBrowser 中删除文档。我做了这个例子来说明我的问题。表单上有两个按钮和一个 webBrowser。一个按钮执行 webBrowser.DocumentText,另一个按钮在 document.DesignMode = "On" 和 "Off" 之间切换。DesignMode 按钮使用“CheckOnClick”。我希望你能看到它的作用。

现在,如果我们在一台装有 IE8 的机器上运行它;然后切换进入和退出 DesignMode 确实将 webBrowser.Document 留在原处。现在,如果我们在一台装有 IE9 的机器上运行它;然后将 DesignMode 设置为“On”或“Off”会导致 webBrowser 文档更改为“。如果 webBrowser 处于 DesignMode = “On”,我们设置 DocumentText;那么 webBrowser 现在处于 DesignMode = “Off”。

我一直无法找到解决此问题的方法,以便能够在 IE9 中同时使用 webBrowser.DocumentText 和 DesignMode。IE8 行为对我有用,而 IE9 则不行。我无法想象我如何能够设置 DocumentText 然后能够对其进行编辑。

是否有设置或解决方法来恢复 IE8 行为?在 IE9 中的同一个文档上使用 DocumentText 和 DesignMode 似乎是不可能的。

提前感谢您的帮助。我花了很多时间来寻找自己的答案,但到目前为止一直无法找到答案。

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        webBrowser1.DocumentText = "<HTML><BODY>Initial text</BODY></HTML>";
    }

    private void designModeToolStripButton_Click(object sender, EventArgs e)
    {
        if (this.designModeToolStripButton.Checked)
            webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "On", null);
        else
            webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "Off", null);
    }

    private void setTextToolStripButton_Click(object sender, EventArgs e)
    {
        webBrowser1.DocumentText = "<HTML><BODY>New text</BODY></HTML>";
    }
}

我还尝试在 WebBrowserDocumentCompleted 事件中设置 DesignMode,并且(自动)发生同样的问题。

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (this.designModeToolStripButton.Checked)
            webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "On", null);
        else
            webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "Off", null);
    }
4

2 回答 2

3

问题是当您设置 DocumentText 时,它会将 designMode 重置为“Inherit”,而当您将 designMode 设置为“On”时,它会清除 DocumentText。这似乎只发生在 IE 9 上。

这个修复对我有用:

webBrowser1.Document.Body.SetAttribute("contentEditable", "true");
于 2011-12-23T18:41:49.907 回答
2

谢谢 Eibrahim .. 它似乎也适用于我的 vb.net 项目。我像这样使用它

我将此代码放在 DocumentCompleted 事件中,它运行良好 Win7+IE8 和 Win7+IE9

    Try
        If WebBrowser1.Document IsNot Nothing AndAlso WebBrowser1.Document.Body IsNot Nothing Then
            WebBrowser1.Document.Body.SetAttribute("contentEditable", "true")
        End If

    Catch ex As Exception
        DumpError(ex)
    End Try
于 2012-09-21T12:23:45.607 回答