我想知道是否可以在 UIWebView 中滚动到给定的 id。
我知道我可以使用以下方法滚动到给定的 (x,y) 点:
[self.myWebView.scrollView setContentOffset:CGPointMake(x, y) animated:YES];
我在本地加载html文件所以我发现html文件中的元素可以被“标记”如下:
<element id="myID">
我想滚动到给定的 id,而不是使用 (x,y) 坐标来滚动,我尝试使用这个:
NSString *yCoordinate = [self.myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myID').value"];
[self.myWebView.scrollView setContentOffset:CGPointMake(0, [yCoordinate intValue]) animated:YES];
但它不起作用。我做错了什么?
提前致谢。
最佳答案
像这样导航到 id :
[self.myWebView stringByEvaluatingJavaScriptFromString: @"window.location.hash='#myID'"];
关于javascript - 在 UIWebView 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387328/