我正在尝试使用 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>
有任何想法吗 ?