简体   繁体   中英

Jquery loading content via ajax

I have an unordered list that will be used as a nav bar

<ul>
            <li><a href="content/index.html" class="selected">Home</a></li>
            <li><a href="content/about.html" class="nav_bubble">About</a></li>
            <li><a href="content/projects.html" class="nav_bubble">Projects</a></li>
            <li><a href="content/tutorials.html" class="nav_bubble">Tutorials</a></li>
</ul>

and I have some javascript to load the content into a div

<script type="text/javascript">
        $(function(){
            $("div#navcontainer > ul > li > a").click(function(e){
                e.preventDefault();
                $.ajax({
                    url: e.currentTarget.href,
                    cache: false,
                    dataType: "html",
                    success: function(data) {
                        console.log(data);
                        $("#content").html(data);
                    }
                });
            });
        });
    </script>

All of that works, the question is, what happens when I load a page from my content folder that has a link in it? It sends your browser to the page instead of loading it into the div. How can I recursivly load all links into the content div instead of sending the user directly to them?

Change:

$("div#navcontainer > ul > li > a").click(function(e){

To:

$("div#navcontainer > ul > li > a, div#content a").live('click',function(e){

"Attach a handler to the event for all elements which match the current selector, now and in the future."

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