ery嵌入WinForms应用程序中以在WebBrowser控件

ery嵌入WinForms应用程序中以在WebBrowser控件

本文介绍了如何将jQuery嵌入WinForms应用程序中以在WebBrowser控件中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WinForms WebBrowser控件中使用jQuery,但没有通过指向URL的链接来访问jQuery(即,我想将jQuery嵌入我的应用程序并从那里获取它).有没有办法做到这一点?如果是这样,那么如何嵌入它(例如作为内容文件)以及使用什么HTML?

I would like to use jQuery in a WinForms WebBrowser control, but without getting access to jQuery via a link to a url (i.e. I want to embed jQuery in my app and get it from there). Is there a way to do that? If so, how does it need to be embedded (e.g. as a Content file) and what's the html to use it?

推荐答案

这似乎很简单.只需抓取文件,将其加载到脚本元素中,然后将其添加到DOM中即可.

It seems pretty straight forward. Just grab the file, load it into a script element, and then add that to the DOM.

这是我的处理方式:

从此处下载: https://code.jquery.com/jquery-2.2.4.min. js 或在这里 https://code.jquery.com/jquery/

Download it from here :https://code.jquery.com/jquery-2.2.4.min.jsor herehttps://code.jquery.com/jquery/

使用文件将其加载到文件中. ReadAllText 然后将其插入DOM.

load it into a file using File.ReadAllTextThen insert it into the DOM.

这是您可以执行的操作:

Here is how you can do that :

    private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = sender as WebBrowser;

        HtmlElement he = wb.Document.CreateElement("script");
        string jquery = System.IO.File.ReadAllText("jquery.js");
        he.InnerHtml = jquery;
        wb.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, he);

    }

您也可以像这样从cdn注入它:

You could also inject it from a cdn like so :

 private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = sender as WebBrowser;

        HtmlElement he = wb.Document.CreateElement("script");
        mshtml.HTMLScriptElement script = he.DomElement as mshtml.HTMLScriptElement;
        script.src = "https://code.jquery.com/jquery-3.1.1.min.js";
        wb.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterEnd, he);

    }

这篇关于如何将jQuery嵌入WinForms应用程序中以在WebBrowser控件中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:11