(void)addImgClickedJsEventToWebView {
// 1、加载完成之后开始注入js事件
[self.webView evaluateJavaScript:@"
function imageClickAction(){
var imgs=document.getElementsByTagName('img');
var length=imgs.length;
for(var i=0;i
img=imgs[i];
img.οnclick=function(){
window.location ='mobile-store-image-preview:'+this.src;
}
}
}
" completionHandler:^(id _Nullable result,NSError * _Nullable error) {}];
// 2、触发方法 给所有的图片添加onClick方法
[self.webView evaluateJavaScript:@"imageClickAction();" completionHandler:^(id _Nullable result,NSError * _Nullable error) {}];
// 3、要拿到所有img标签对应的图片的src
[self.webView evaluateJavaScript:@"document.getElementsByTagName('img').length" completionHandler:^(id _Nullable result,NSError * _Nullable error) {
// 3.1、拿到img标签的个数
NSUInteger length = [result integerValue];
// 3.2、遍历标签,拿到标签中src的内容
for (int i =0 ; i < length; i++) {
NSString *jsStr = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].src",i];
[self.webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result,NSError * _Nullable error) {
EOADetailLog(@"%@",result);
// 3.3、将标签中src内容存入数组
if ([self.imgsArray containsObject:result] == NO) {
[self.imgsArray addObject:result];
}
}];
}
}];
}