[英]Injecting JQuery function in Objective-c in UIWebView
我在jquery中将此函数另存为target.doc中的doc2.js,并且已在捆绑资源中复制了以下内容:
doc2.js:
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideToggle("slow");
});
});
在我的Xcode中,我有以下内容:
UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"new" ofType:@"html"]isDirectory:NO]]];
webView.delegate=self;
[self.view addSubview:webView];
和这种方法:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *jqueryCDN = @"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
NSData *jquery = [NSData dataWithContentsOfURL:[NSURL URLWithString:jqueryCDN]];
NSString *jqueryString = [[NSMutableString alloc] initWithData:jquery encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jqueryString];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"doc2" ofType:@"js" inDirectory:NO];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jsString];}
在我的html文件中:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>Any Thing.</p>
<p>Any Thing.</p>
</div>
<p class="flip">Show/Hide Panel</p>
</body>
</html>
这段代码应该可以处理UIWebView,但是不能和我一起工作。我认为我的jQuery函数没有完成或类似的工作。
您可能想尝试另一种方法:包括本地副本或从CDN下载jquery。 两者都似乎没有必要。
此外:只需在doc2.js中定义一个javascript函数,将文件包含在HTML中,然后直接在Cocoa中调用即可。 确保您使用
isSelectorExcludedFromWebScript
可以拨打电话
evaluateWebScript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.