问题描述
我正在通过 loadHTMLString 将一串 HTML 加载到 UIWebView 中.它会生成一个很长的网页.加载该字符串后,我需要导航到带有名称"的 HTML 锚标记.属性集.在 HTML 中,我可能有:
I'm loading a string of HTML into a UIWebView through loadHTMLString. It produces a very long webpage. Once that string is loaded, I need to navigate to an HTML anchor tag with its "name" attribute set. In the HTML, I might have:
//3 pages of text here
<a name="go here"></a> lots more text here
//another 3 pages of text here
我需要向下滚动网页以转到此处";一旦加载.如果用户单击网页内的链接并加载外部 URL,则映射工作正常.但在这种情况下,我需要首先向下滚动已加载的网页.
I need the webpage to scroll down to "go here" once loaded. The mappings work fine if a user clicks a link inside of the webpage and it loads an external URL. But in this case, I need to initially scroll down an already loaded webpage.
我仍然可以像这样执行 JavaScript 代码:
I can still execute JavaScript code like this:
[MyWebView loadHTMLString:dataString baseURL:[NSURL URLWithString:@"http://www.myscheme.com"]];
[webView stringByEvaluatingJavaScriptFromString:@"javascriptFunc('param1');"];
但我需要类似的东西来导航到锚标记.有什么建议吗?
But I need something similar for navigating to the anchor tag. Any suggestions?
推荐答案
您可以通过执行以下操作跳转到已加载 HTML 文件中的锚点:
You can jump to an anchor in an already-loaded HTML file by doing:
[webView stringByEvaluatingJavaScriptFromString:@"window.location.hash='#foo'"];
这将完成跳跃,没有动画.
This will do the jump, sans animation.
这篇关于如何引用 HTML 锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!