简体   繁体   中英

Opening a link within the same page using javascript or ajax

What I have is a list of links in my left sidebar and a main content area to the right, I want to be able to click the links to the left and have each "page" open in the content area, instead of reloading a new page. I'm sure this is fairly easy using javascript or ajax, I'm just not sure how. Thanks in advance for your help!

Say you have

<div id='content'></div>

on the right side and a link on the left

<a href='#' id='contact_link'>Contact</a>

You can use JQuery's load function to load the div with the content of a page when you click the link:

$("#contact_link").click(function() {
    $("#content").load("contact.html");
});

If you're not using JQuery you can get it here , otherwise there are ways to do it in plain JS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM