如何引用HTML锚点? [英] How to reference HTML anchor?
问题描述
我通过loadHTMLString将一个HTML字符串加载到UIWebView中。它产生一个很长的网页。加载该字符串后,我需要导航到设置了name属性的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
我需要网页加载后向下滚动到go here。如果用户单击网页内部的链接并加载外部网址,则映射可以正常工作。
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 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'"];
这将跳转,sans动画。
this will do the jump, sans animation.
这篇关于如何引用HTML锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!