如何使用css,js和图像调用HTML文件

How to call HTML file with css, js and images?

本文关键字:图像 调用 HTML 文件 js 何使用 css      更新时间:2023-09-26

我正在从服务器下载zip文件。在NSCachesDirectory中,我有html文件,我想知道如何使用他们的源文件运行html文件,我已经粘贴了我的代码。请帮助我

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsPath =  [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"/Lesson-file/Lesson%d",[str_lsn_id integerValue]]];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"story_html5" ofType:@"html" inDirectory:documentsPath]];
[lsn_play_webview loadRequest:[NSURLRequest requestWithURL:url]];

这是适合您的方法。从您说的上下文中,您有源文件jscss.

这里的问题可能是因为在运行时这些文件未加载或编译器无法找到它们的位置。

例如:

  <!--For CSS file html tag is-->
  <link rel="stylesheet" href="styles.css"> 
  <!--For JS file html tag is-->
  <script type="text/javascript" src="exmample.js"></script>

因此,您必须手动提供这些文件的位置。

请参阅此处的标签 hrefsrc .这些是资源文件的位置,当 html 页面加载到内存中时,这些文件在运行时需要。

如果这些文件位于同一目录中,则无需提供 url

建议:

建议您在此处提供文件位置 URL。编辑您的 html 文件并为网址标记添加标记。

示例代码:

<html> <head>
        <script type="text/javascript" src="#JS-FILE1#"></script>
        <link rel="stylesheet" href="#CSS-FILE1#">
</head> <body> </body>
</html>

您可以看到我已经用标记#JS-FILE1##CSS-FILE1#替换了文件。现在我们将在加载 html 文件之前用实际的 url 替换这些标记。

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Location of Resource files
NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"exmample" ofType:@"js"];
NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:@"stylesheet" ofType:@"css"];
html = [html stringByReplacingOccurrencesOfString:@"#JS-FILE1#" withString:jsFilePath];
html = [html stringByReplacingOccurrencesOfString:@"#CSS-FILE1#" withString:cssFilePath];

在此处放置断点并调试输出字符串。 NSLog这个字符串复制它并创建临时文件 - 在浏览器中打开它以交叉检查所有资源是否已加载?

如果不是,请检查资源文件的 url 是否正确。