当html由字符串生成时,如何在c#webview中引用jquery

本文关键字:c#webview jquery 引用 html 字符串 | 更新日期: 2023-09-27 18:24:33

我需要引用我的resources文件夹"/resources/jquery.js"中的jquery文件。当我将其加载到web视图中时,会出现html,但jquery不起作用。我正在使用visual studio 2013,希望它能在windows phone 8.1上运行,以下是我尝试做的。

private void mWebview_Loaded(object sender,RoutedEventArgs e){
String html="<!DOCTYPE html>'r'n<html>'r'n<head>'r'n    <script src='"/Resources/jquery.js'">'r'n    </script>'r'n    <script>'r'n    $(document).ready(function(){'r'n        $('"p'").click(function(){'r'n        $(this).hide();'r'n        });'r'n    });'r'n    </script>'r'n</head>'r'n<body>'r'n    <button onclick='"show()'">Press!</button>'r'n    <p id='"p'">Dummy</p>'r'n    <p>If you click on me, I will disappear.</p>'r'n    <p>Click me away!</p>'r'n    <p>Click me too!</p>'r'n'r'n</body>'r'n</html>"
mWebView.NavigateToString(html);
}

如果字符串太复杂而无法读取,请复制http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide但我已经将脚本的源属性替换为我的jquery文件所在的位置。

在手机应用程序中使用webview时,有没有具体的方法需要做到这一点?

当html由字符串生成时,如何在c#webview中引用jquery

如果Resources文件夹与正在处理的文件位于同一目录中,则必须删除/。您编写的路径,在HTML中表示../Resources/jquery.js

不管怎样,把整个html放在一个变量中是非常难看的,你可以在你的项目中创建一个Page.html,然后

WebView.Navigate(new Uri("ms-appx-web:///Page.html", UriKind.Absolute));

修复此问题的方法是在源代码中,您需要使用前缀ms-appx-web://,因此源代码现在看起来是这样的。src="ms-appx-web:///Resources/jquery.js"

这也适用于引用本地css文件。

我希望这能帮助未来的人们