在UIWebView的Objective-c中注入JQuery函数
Injecting JQuery function in Objective-c in UIWebView
我在 jquery 中有这个函数保存为 doc2.js在我的目标中,我的捆绑资源中有副本如下:
文档2.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
相关文章:
- jQuery将TD注入适当的列(俄罗斯方块风格)
- jQuery模式窗口在内容为“”时不居中;注入“;在飞行中
- 将html注入<李>使用js或jquery
- 注入的元素和jQuery脚本.如何让他们一起工作
- JQuery load() 注入到 DIV 中的 PHP 文件能否找到有关此 DIV 的任何信息
- 使用JQuery将ejs模板注入到html中
- 注入SVG元素并使用jquery访问它
- 避免在javascript中的jquery被注入到某个网页的情况下发生冲突
- 注入jquery会完全改变页面结果——这是一种糟糕的方式
- 重新加载页面以显示注入的 jQuery UI 元素
- 在UIWebView的Objective-c中注入JQuery函数
- jQuery文档是否准备好等待通过Backbone动态注入的html元素
- 当子窗口准备就绪时,使用 jQuery 将 HTML 从父窗口注入子窗口
- 使用 Chrome 扩展程序将 jQuery 注入 Google.com
- Firefox DevTools:自动注入jQuery
- RequireJs-为什么注入Jquery而没有注入JSZip
- 向angular js组件注入jquery和下划线
- 在TypeScript函数中注入jQuery
- JavaScript依赖注入+ jQuery
- 注入jQuery, jQuery UI和插件,没有版本冲突