Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I navigate through the menu I want only the contentarea div to change and not reload the whole page. I want to use the jquery load function to read a div from another page (from members.html div colTwo) and insert it onto the page (into div contentarea). I have been trying but couldn't get it to work.

Any help editing this script would be helpful!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>
<link href="default.css" rel="stylesheet" type="text/css" />
</head>

<script type="text/javascript" src ='http://code.jquery.com/jquery-1.9.1.min.js'</script>
<script type="text/javascript">
$(document).ready(function() {
function memberspage(){
    $('#contentarea').load('members.html #colTwo');
}
});
</script>

<body>
<div id="header">
    <h1>My Title</h1>
</div>
<div id="content">

    <div id="colOne">

        <div id="menu1">
            <ul>
                <li><a href="#" onclick='memberspage()'>Members</a></li>
            </ul>
        </div>
        <div class="margin-news">
            <h2>Recent Updates</h2>
            <p><strong>None At This Time</strong> </p>
        </div>
    </div>

    <div id="contentarea"><h2>Starting Text</h2></div>
    <div style="clear: both;">&nbsp;</div>
</div>

</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

The problem is that you've declared your memberspage() function inside a document ready handler, which means that it is not in scope when you try to call it from an inline onclick handler on your anchor element - inline attributes like that can only access global functions.

You can fix this by moving the function declaration out of the document ready (making it global), but a better option would be to bind the click handler with jQuery:

$(document).ready(function() {
    $("#menu1 a").click(function(e){
        $('#contentarea').load('members.html #colTwo');
        e.preventDefault();
    });
});

I've added e.preventDefault() to stop the default behaviour on the anchor click which might otherwise scroll back to the top of the page. (In an inline handler you would get the same effect by returning false.)

Note also that your <script> element that includes jquery.js is missing a closing > right before the closing </script>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...