This article mainly introduces jquery's method of getting File content through load and jumping to the anchor. it involves the usage skills of load, animate, and other methods, for more information, see the following example. Share it with you for your reference. The specific analysis is as follows:
Yesterday, I made a page similar to the help document type, with navigation on the left and content on the right. I was planning to use iframe to implement the content display area on the right, but I used the load method in jquery ajax to adapt to the height of iframe.
It is easy to obtain the content in a remote file. you can directly use the jquery load method:
$("#content").load("xxx.aspx")
In this way, it is easy to put the content in the xxx. aspx file in the tag with id as content. Now I want to achieve the following effect: when I get the file content, I want to jump to the corresponding anchor, so I want to use jquery's scrollTop. for example, after I get the file content, tag to be adjusted to id = "name:
$("body,html").animate({scrollTop:$("#name").offset().top});
Offset () is to get the relative offset of the matching element in the current view, $ ("# name "). offset (). top is the relative offset of the tag whose ID is name from the current view to the top. The above code should be written in the following way:
$(function(){ $("#content").load("xxx.aspx",function(){ $("body,html").animate({scrollTop:$("#name").offset().top}); });})
To avoid clicking and navigation to send requests to the server, we can store the data obtained each time.
Of course, this method is only applicable to pages that do not consider SEO optimization.
I hope this article will help you with jQuery programming.