0, 你需要懂點這些:
getElementsByTagName 獲取同類的標簽集合
getElementsByClassName 獲取class
getElementById 獲取id
getElementsByName 帶有指定名稱的對象的集合
getElementsByTagNameNS 帶有指定名稱和命名空間的所有元素的一個節點列表
1, oc 給 webView添加js, 調用所添加的方法
//獲取上下文
JSContext *context = [self.myWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//寫js方法
NSString *js = @"function showAlert(){\
var obj = document.getElementsByTagName('input');\
for (var i = 0; i < obj.length; i++) {\
input = obj[i];\
if(input.type == 'button'){\
input.style.webkitAppearance = 'none';\
}\
}}\
";
//注入方法,調用方法
[context evaluateScript:js];
[context evaluateScript:@"showAlert()"];
2, oc 攔截close 方法, 執行其他的
//獲取上下文
JSContext *context = [self.myWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
__weak typeof(self) weakSelf = self;
context[@"close"] = ^() {
[weakSelf.navigationController popViewControllerAnimated:YES];
};
3, 獲取div中的img元素
var obj = document.getElementById('pause').getElementsByTagName('img');
obj[0].style.height = '10px';
console.log(obj);