我可以在应用程序包中访问jquery和cordova的Documents目录中创build一个html文件吗?
我正在build立一个存储在HTML文件
[[PROJ UserDirectory] stringByAppendingPathComponent:@"tmp_Form_File.html"]
并在文件“tmp_FOrm_File.html”内部dynamic构build。 正文显示正确,但JS不会执行。
我正在试着做:
NSString * cordova_path = [[NSBundle mainBundle] oathForResource:@"cordova-2.2.0" ofType:@"js" inDirectory:@"html"] NSString = [[@"<script type=\"text/javascript\" src=\"" stringByAppendingString:cordova_path] stringByAppendingString:@"\"></script>"];
和jquery类似。
我的问题是,我不认为当我加载到一个webView的htmlfile它相应地获取数据。
我testing了琴弦,他们看起来很健康:
<script type="text/javascript" src="/var/mobile/Applications/[DATA]/PROJ.app/html/cordova-2.2.0.js"></script><script type="text/javascript" src="/var/mobile/Applications/[DATA]/PROJ.app/html/jquery-1.7.2.min.js"></script><script type="text/javascript">$(function(){ alert("test"); });</script>
1)我做错了吗? 如果是的话,我该如何解决这个问题?
2)通过ios将包中的html目录复制到Documents是否更好? 如果是这样,怎么样?
编辑:暂时删除jQuery的引用,jQuery和cordova。 只是使用纯JavaScript,并会正确提醒。 所以这意味着Bundle中的src有问题。
这是因为您的参考不起作用。 当你想让你的HTML在webview中呈现一些相对path的时候,你可以使用选项“为添加的文件夹创build文件夹引用”来添加它。
如果这个html页面不会获取并存储任何数据,那么将其保存在bundle中,否则将它们复制到Appdelegate中的Library或文档文件夹中。
希望这可以帮助!
我不能引用它atm,但我解决了这个问题: – 我把[BUNDLE] / html的内容复制到文件….
NSString * sourcePath = [[[NSBundle mainBundle] pathForResource:@"cordova-2.2.0" ofType:@"js" inDirectory:@"html"] stringByDeletingLastPathComponent]; NSString * documentDirectory = [AssetInspectorFileManager UsersDirectory];
-
调整了javascript的src。
<script type = \“text / javascript \”src = \“cordova-2.2.0.js \”>
<script type = \“text / javascript \”src = \“jquery-1.7.2.min.js \”>
-
closures了缺less的括号
“$(\”div.container \“)。css(\”z-index \“,1;”
成为
" $(\"div.container\").css(\"z-index\",1);"
-
当你写数据的时候,这样做是不好的,比如:
$(this).click(function(){// [this is a comment] alert(“hi”);});
所以你必须针对这样的问题进行调整。