UIWebView如何侦测< img src =" ..." />被挖掘 [英] How UIWebView detect <img src="..." /> is tapped
问题描述
我知道下面的方法可以检测到链接元素点击。但是我想知道 UIView
是否可以检测到是否点击 img
元素?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求导航类型:(UIWebViewNavigationType)navigationType {
switch(navigationType) {
case UIWebViewNavigationTypeLinkClicked:
NSLog(@link is click);
//做某事
break;
默认值:
break;
}
返回true;
}
一些html代码如下:
< p> xxxxxxxxx< / p>
< p>< img src =http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg> < / p为H.
< p> xxxxxxxxx。< / p>
< p>< img src =http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpg> < / p为H.
< p> xxxxxxxxx。< / p>
你能告诉我如何使用示例代码吗?谢谢。
您的img标签没有html点击事件。如果您可以像下面这样制作图片 - < p> xxxxxxxxx< / p>
< p>< img src =http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpgonclick =myfunction()> < / p为H.
< p> xxxxxxxxx。< / p>
< p>< img src =http://imgs.ebrun.com/resources/2015_06/2015_06_16/201506167911434413787546.jpgonclick =myfunction()> < / p为H.
< p> xxxxxxxxx。< / p>
然后,您可以在UIWebView Delegate方法中捕获您的img click事件 - shouldStartLoadWithRequest。
其他可能的选项 -
否则,您已经编写了一个JavaScript方法,它将OnClick方法动态添加到所有IMG标签。当你的页面加载到UIWebView中时调用这个javascript方法 -
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//执行javascript方法或纯JavaScript如果需要的话
[_webView stringByEvaluatingJavaScriptFromString:@methodName();];
}
I know that method below can detect a link element tap. But I want to know if the UIView
can detect if the img
element is tapped?
- (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;
}
Some html source like below:
<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>
Could you show me how to do it using sample code? Thanks.
Your img tag does not have html click event. If you can make your image like below way -
<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>
Then you can catch your img click event within UIWebView Delegate method - shouldStartLoadWithRequest.
Alternate possible option -
Otherwise you have write a javascript method which dynamically add OnClick method to all IMG tags. And call this javascript method when your page load in UIWebView -
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
//Execute javascript method or pure javascript if needed
[_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
}
这篇关于UIWebView如何侦测< img src =" ..." />被挖掘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!