0

我正在尝试使用 UIWebview 元素的以下语句来停止 html5 视频

[webView stringByEvaluatingJavaScriptFromString: @"document.querySelector('video').pause();"];

但它对我不起作用。我尝试了带有 onclick 事件的 javascript 代码,它可以工作。

这里是objective-c代码

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// encode the string
NSString * result = [[[request URL]absoluteString]
                     stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// extract the path components from the result string
NSArray *pathElements = [[NSArray alloc] initWithArray:[result pathComponents]];

// when click the a tag the video should stop 
if([pathElements[[pathElements count] -1] isEqualToString:@"tester"]){
    [webView stringByEvaluatingJavaScriptFromString: @"document.querySelector('video').pause();"];
    }
}

这里是html5代码

<video width="720px" height="396px" controls="controls" >
        <source src="Video.m4v" type="video/mp4">
</video>
<!-- a tag only for test purpose -->
<p><a href="/tester" target="_blank">TESTLINK</a></p>

有任何想法吗 ?

4

1 回答 1

0

shouldStartLoadWithRequest 在页面加载之前被调用,它询问你是否 shouldStartLoadWithRequest

UIWebViewDelegate 协议参考

webView:shouldStartLoadWithRequest:navigationType:

在 Web 视图开始加载框架之前发送。

因此,您可能需要一个按钮和一个工具栏,或其他方式来触发在页面加载后执行代码的方法,您还应该确保“document.querySelector('video')”实际上返回视频标签时叫。

于 2013-05-16T03:45:17.407 回答