Jquery uses load to get the file content and jump to the anchor. method _ jquery

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.