使用 WebBrowser.DocumentText 和 DomDocument.DesignMode 的应用程序在 IE8 中工作,在 IE9 中不起作用

发布于 2024-12-01 00:08:09 字数 2169 浏览 1 评论 0原文

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

现在,如果我们在装有 IE8 的机器上运行它;然后切换设计模式确实会将 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);
    }

Yes my use of webBrowser control work fine in IE8 and not in IE9. It seems that setting the HTMLDocument from DesignMode = "On" to DesignMode = "Off" removes the document from the WebBrowser. I made this example that shows my problem. There are two buttons and one webBrowser on a form. One button does the webBrowser.DocumentText and the other button toggles between document.DesignMode = "On" and "Off". The DesignMode button uses "CheckOnClick". I hope that you can see what it does.

Now if we run this on a machine with IE8; then toggling in and out of DesignMode does leave the webBrowser.Document in place. Now if we run this on a machine with IE9; then setting DesignMode to "On" or to "Off" causes the webBrowser document to change to ". If the webBrowser is in DesignMode = "On", and we set the DocumentText; then the webBrowser is now in DesignMode = "Off".

I have been unable to find a way to work around this behavior to be able to use webBrowser.DocumentText and DesignMode at the same time in IE9. The IE8 behavior works for me and the IE9 does not. I am unable to imagine how I might be able to set the DocumentText and then be able to edit it.

Is there a setting or work around to get the IE8 behavior back? It seems imposible to be able to use DocumentText and DesignMode on the same document in IE9.

Thanks in advance for any help. I have taken a lot of time to search no my own to find an answer, and have been unable so far.

    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>";
    }
}

I also tried doing the setting of DesignMode in the WebBrowserDocumentCompleted event, and the same problem happens (automatically).

        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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我早已燃尽 2024-12-08 00:08:10

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

这个修复对我有用:

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

The problem is that when you set DocumentText, it resets designMode to "Inherit" and when you set designMode to "On", it clears DocumentText. This seems to only happen on IE 9.

This fix worked for me:

webBrowser1.Document.Body.SetAttribute("contentEditable", "true");
梦过后 2024-12-08 00:08:10

谢谢 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

Thanks Eibrahim.. it seems to work for me as well in my vb.net project. I used it like

I put this code in DocumentCompleted event and it worked well Win7+IE8 and 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文