链接到本地​​HTML文件,链接到JS和CSS [英] Linking to a local HTML file with links to JS and CSS

查看:184
本文介绍了链接到本地​​HTML文件,链接到JS和CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的双页面网站,我想创建一个应用程序。



我目前有一个UIWebView设置为接收本地HTML文件: / p>

   - (void)viewDidLoad {
[super viewDidLoad];
NSString * file = [[NSBundle mainBundle] pathForResource:@indexofType:@html];
NSURL * filePath = [NSURL fileURLWithPath:file];
NSURLRequest * requestObj = [NSURLRequest requestWithURL:filePath];
[webView loadRequest:requestObj];
}

这在加载文件时效果很好;然而,该网站有附加到它的JQuery插件和CSS样式表,被iPhone忽略。我不知道我是否已经正确导入了这些文件,所以这可能是问题的一部分(我仍然不是100%关于X-Code如何链接到文件)。



如果X代码(脚本)中的组中有一个名为slider.js的文件,我可以使用html链接到脚本/ slider.js,还是仅显示环境的组部分?如果是这样,我怎么链接到这样的文件夹结构的文件?



欢呼你的帮助。

加载您的请求

   - (void)loadHTMLString:(NSString * )string baseURL:(NSURL *)baseURL 

重要的部分是基本网址:它告诉webview你的文体根源在哪里。相对于baseURL,你可以在html中链接你的文件。假设您的jQuery和CSS文件也在您的包中。



放置一个实际文件夹(将文件夹拖到您的项目文件夹树中,然后选择为任何文件夹创建文件夹参考添加文件夹它会变成蓝色)在你的项目根目录像htdocs。把你的html的东西放入它并设置baseURL到那个htdocs文件夹。


I have a simple two page website that I would like to create an app for.

I currently have a UIWebView set up to receive a local HTML file:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *filePath = [NSURL fileURLWithPath:file];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
    [webView loadRequest:requestObj];
}

This works fine in loading the file; however the website has JQuery plugins and CSS stylesheets attached to it that are being ignored by the iPhone. I've no idea if I've imported the files correctly so this may be part of the problem (I'm still not 100% on how X-Code links to files).

If a group in X-Code (scripts) has a file in it called slider.js could I use the html to link to scripts/slider.js or is the groups part of the environment for display only? If so, how do I link to a file that's in a folder structure like this?

Cheers for your help.

解决方案

Load your request with

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

The important part is the base URL: It tells the webview where your docroot is. Relative to the baseURL you can link your files within the html. Assuming that your jQuery and CSS files are in your bundle as well.

Put a real folder(drag a folder to your projects folder tree and select "Create Folder Reference for any added folders" it will turn blue) in your project root like htdocs. Put your html stuff in it and set the baseURL to that htdocs folder then.

这篇关于链接到本地​​HTML文件,链接到JS和CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆