原文鏈接: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];