为什么javascript'window.location.hash'在ios11中的WKWebview中不起作用?

时间:2017-09-21 00:30:19

标签: javascript objective-c wkwebview ios11 xcode9

它可以在ios10或之前使用,但它无法在ios11中运行?

- (void)jumpToHash:(WKWebView *)wkWebView {
NSString * jsJumpToHash =[NSString stringWithFormat:@"function jumpToHash(){window.location.hash ='#%@hash';};",self.hashLocation];

[wkWebView evaluateJavaScript:jsJumpToHash completionHandler:^(id Result, NSError * error) {}];

NSString *jsFun = @"jumpToHash()";

[wkWebView evaluateJavaScript:jsFun completionHandler:^(id Result, NSError * error) {}];

self.hashLocation = @"";
}

1 个答案:

答案 0 :(得分:3)

在iOS11中,window.location.hash不支持滚动到位置。

请使用以下代码将滚动网页滚动到所选位置

[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var hashElement=document.getElementById('%@');if(hashElement) {hashElement.scrollIntoView();}", href]];