【问题标题】:App Using WebBrowser.DocumentText And DomDocument.DesignMode Works In IE8 And Does Not Work In IE9使用 WebBrowser.DocumentText 和 DomDocument.DesignMode 的应用程序在 IE8 中有效,在 IE9 中无效
【发布时间】:2011-08-22 04:25:52
【问题描述】:

是的,我对 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);
    }

【问题讨论】:

    标签: browser domdocument designmode


    【解决方案1】:

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

    这个修复对我有用:

    webBrowser1.Document.Body.SetAttribute("contentEditable", "true");
    

    【讨论】:

    • 如果我能给 100 分,你会明白的——我在整个互联网上查找了解决方案,这是第一个(也是唯一一个?)有效的解决方案!致敬,伙计!
    • 我同意 Acme,这是唯一有效的方法....我已经为此奋斗了好几天了。
    【解决方案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
    

    【讨论】:

    • 在这个网站上感谢某人的方式是接受他/她的回答。您可以通过单击答案左侧的检查来做到这一点。
    • 谢谢兰詹!你也有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多