简体   繁体   中英

Jquery Load Not Loading after going to another page

I have an html page where a JQuery Loads a page into a div on click. I can click on it and it goes to page 2 and it works. I have the same link that goes back to page 1 and it will load the html in page 1, but if I click back to page 2 it will not load. So pretty much I cannot go back and forth on loading a div from Jquery. I have search and seen something on using live() but can't seem to get anything to work. Any Suggestions?

My Jquery Function to load the div from another page.

<script>
function changepage(page) {
    $(function(){
        $('#maincontent').load('index.php?p='+page);
        return false;
    });
}
</script>

HTML To load the page 2:

<a href="#" onclick="changepage('page2');">Page 2</a>

HTML To load back to page 2

<a href="#" onclick="changepage('page1');">Page 1</a>

EDIT: Entire Page:

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>My Page</title>


</head>

<body>
<div id="wrapper">
        <!-- Page Content -->
        <div id="page-wrapper">
            <div class="container-fluid">
            <div id="maincontent">

            <a href="#" onclick="changepage('page2');">Page 2</a>


                </div>
                <!-- /.maincontent -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->


<script>
function changepage(page) {
    $(function(){
        $('#maincontent').load('subpage.php?p='+page);
        return false;
    });
}
</script>

</body>

</html>

jQuery是否链接到该页面?

I could be wrong, but I don't think you've given us enough information here. The above code looks fine. Is input.php working correctly? What happens when you call:

http://[domain]/index.php?p=page1
http://[domain]/index.php?p=page2

From a browser? Does it output a whole page, or just #maincontent? It's kind of weird, does the index page output a single div, or are you stuffing a whole html page inside another one? Sound like it could be the output from index.php, which would normally be the landing page of your site, so why are you stuffing the whole landing page inside a div?

I agree with tokyovariable . you need to link jquery before using it in function. Try adding
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> before script with changepage function.
Also make sure jquery is loaded before changepage function is called. Try
function changepage(page) { $(document).ready(function(){ $('#maincontent').load('index.php?p='+page); return false; }); }

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