我想在WinForms WebBrowser控件中使用jQuery,但不能通过指向url的链接访问jQuery (即,我想将jQuery嵌入到我的应用程序中并从中获取)。有没有办法做到这一点?如果是这样,它需要如何嵌入(例如,作为内容文件),以及使用它的html是什么?
发布于 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中。
下面是你如何做到这一点:
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注入它:
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);
}
https://stackoverflow.com/questions/42095271
复制相似问题