iOS的webView调用jQuery的方法

来源:互联网 发布:统计模型的数据选择 编辑:程序博客网 时间:2024/05/22 10:31

原文链接:http://blog.csdn.net/hjaycee/article/details/48728507


大部分人都知道可以用- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;调用js代码,但是有时我们需要使用功能更强大的jQuery,可是oc有没有提供明确的方法,这时该怎么办呢?


请看下面的截图,这里就是oc调用jQuery的方法:

先在winodw.onload方法中创建<script>标签,该标签引用jQuery,然后将该标签追加到head中,再使用定时器延迟执行含有jQuery的函数。


下面附上转换成OC代码的源码:

NSString *commond = @"window.onload=function(){var jq=document.createElement(\"script\");jq.setAttribute(\"src\",\"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/jquery-1.10.2.js\");document.getElementsByTagName(\"head\")[0].appendChild(jq);window.setTimeout(\"exec()\",1000)};function exec(){document.write($(\"body\").get(0))};";
[self.webView stringByEvaluatingJavaScriptFromString:commond];


1 1
原创粉丝点击