在UIWebView的內(nèi)容加載完之后匙头,可以利用javascript獲取其頁(yè)面內(nèi)的數(shù)據(jù)漫谷,核心就是通過(guò)UIWebView的方法:
NSString *string = [awebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('field_2').value;" ];
以下是一些常用的js腳本:
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
thisTitle = document.title;
thisProtocol = document.location.protocol;
thisPort = document.location.port;
thisHash = document.location.hash;
thisSearch = document.location.search;
thisPathname = document.location.pathname;
thisHtml = document.documentElement.innerHTML;
thisBodyText = document.documentElement.innerText;//獲取網(wǎng)頁(yè)內(nèi)容文字
thisBodyText = document.body.innerText;//獲取網(wǎng)頁(yè)內(nèi)容文字
也可以通過(guò)同樣的方法去設(shè)置頁(yè)面內(nèi)容(比如幫用戶(hù)輸入表單數(shù)據(jù))
比如:
NSString *string = [awebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('field_2').value='a value';" ];
就可以修改field_2的值了
同樣也可以去模擬頁(yè)面內(nèi)按鈕的點(diǎn)擊,提交頁(yè)面蹂析,比如:
document.getElementById('aButtonName').click();
或者舔示,假設(shè)知道按鈕是第幾個(gè)input標(biāo)簽(假設(shè)為第一個(gè))
document.getElementsByTagName('input').item(0).click();
也可以設(shè)置checkBox的狀態(tài):
document.getElementById('aCheckBoxId').checked=true;
參考:
http://hi.baidu.com/zfpp25/blog/item/0bc5e3565a7e632e0cf3e3d7.html