UIWebView 如何检测 <img src="..." /> 被点击

How UIWebView detect <img src="..." /> is tapped

我知道下面的方法可以检测到 link 元素点击。但是我想知道 UIView 是否可以检测 img 元素是否被点击?

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
switch (navigationType) {
    case UIWebViewNavigationTypeLinkClicked:
        NSLog(@"link is click");
        //do something
        break;
    default:
        break;
   }

    return true;
}

一些 html 来源如下:

<p>xxxxxxxxx</p>
<p><img src="http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg"> </p>
<p>xxxxxxxxx。</p>
<p><img src="http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg"> </p>
<p>xxxxxxxxx。</p>

你能用示例代码告诉我如何做吗?谢谢。

您的 img 标签没有 html 点击事件。如果你能像下面这样制作你的图像 -

<p>xxxxxxxxx</p>
<p><img src="http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg" onclick="myfunction()"> </p>
<p>xxxxxxxxx。</p>
<p><img src="http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg" onclick="myfunction()"> </p>
<p>xxxxxxxxx。</p>

然后您可以在 UIWebView 委托方法 - shouldStartLoadWithRequest 中捕获您的 img 点击事件。

其他可能的选项 -

否则你已经编写了一个 javascript 方法,它动态地将 OnClick 方法添加到所有 IMG 标签。当您的页面在 UIWebView 中加载时调用此 javascript 方法 -

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    //Execute javascript method or pure javascript if needed
    [_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
}

也许this回答可以帮到您。基本上你需要将标签包装在标签内并处理

内的按钮点击事件
webView:shouldStartLoadWithRequest:navigationType

方法