How to reference HTML anchor?

雨燕双飞 提交于 2019-12-31 01:43:54

问题


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

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.

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?


回答1:


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.




回答2:


If I'm not mistaken, just add #go%20here to the end of the URL

EDIT: ahh you mean already loaded. Is it really out of the question to just do reload the page? The browser should have cached most of it anyways...



来源:https://stackoverflow.com/questions/760584/how-to-reference-html-anchor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!