如何引用HTML锚点?

4th*_*ace 4 html javascript iphone cocoa-touch uiwebview

我正在通过loadHTMLString将一串HTML加载到UIWebView中.它会产生一个很长的网页.加载该字符串后,我需要导航到设置了"name"属性的HTML锚标记.在HTML中,我可能有:

//3 pages of text here
<a name="go here"></a> lots more text here
//another 3 pages of text here
Run Code Online (Sandbox Code Playgroud)

我需要网页在加载后向下滚动到"go here".如果用户单击网页内的链接并加载外部URL,则映射可以正常工作.但在这种情况下,我需要首先向下滚动已加载的网页.

我仍然可以像这样执行javascript:

[MyWebView loadHTMLString:dataString baseURL:[NSURL URLWithString:@"http://www.myscheme.com"]]; 
[webView stringByEvaluatingJavaScriptFromString:@"javascriptFunc('param1');"];
Run Code Online (Sandbox Code Playgroud)

但我需要类似的东西导航到锚标签.有什么建议?

小智 10

您可以通过执行以下操作跳转到已加载的HTML文件中的锚点:

[webView stringByEvaluatingJavaScriptFromString:@"window.location.hash='#foo'"];
Run Code Online (Sandbox Code Playgroud)

这将做跳跃,没有动画.