首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在WinForms应用程序中嵌入jQuery以在WebBrowser控件中使用

如何在WinForms应用程序中嵌入jQuery以在WebBrowser控件中使用
EN

Stack Overflow用户
提问于 2017-02-08 00:37:51
回答 1查看 2.4K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-10 05:53:21

这看起来很简单。只需抓取文件,将其加载到脚本元素中,然后将其添加到DOM中。

下面是我的方法:

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

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

下面是你如何做到这一点:

代码语言:javascript
复制
    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注入它:

代码语言:javascript
复制
 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);

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42095271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档