我正在使用 WebView 将本地引用的 HTML 文件加载到我的应用程序中。我有文件夹结构:
-assets/
-www/
-css/
-images/
-js/
-index.html
index.html 使用相对路径链接到各种 css、javascript 和图像文件,例如:
<script src="js/functions.js" type="text/javascript"></script>
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" >
因为我使用的是相对路径,所以我通过在 ViewController.h 中使用这段代码将 HTML 内容加载到 WebView 中
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"assets/www"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
当我运行 iOS 模拟器时,页面成功加载并显示页面上图像文件夹中的图像。但是,CSS 和 Javascript 文件不会加载。我哪里错了?
最佳答案
像这样更改文件 .js 和 .css 的路径:
<script src="../js/functions.js" type="text/javascript"></script>
<link href="../css/style.css" media="screen" rel="stylesheet" type="text/css" >
关于javascript - CSS 和 Javascript 未在 iOS webview 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195356/