Replace div content using jQuery

Alin-Mihai Bobeica

I have this code in header.html:

<nav role="navigation">
    <ul class="nav">
        <li><a id="home" href="index.html" class="current">home</a></li>
        <li><a id="about" href="includes/test.html">about</a></li>
        <li><a id="resources" href="#">resources</a></li>
        <li><a id="archive" href="#">archives</a></li>
    </ul>
 </nav>

Now, everything seems fine until my index.html loads files from another folder to fill the divs with id="header", id="content" and id="footer" using:

$(function() {
    $("#mainhead").load("templates/header.html");
    $("#content").load("templates/content.html");
    $("#footer").load("templates/footer.html");
});

My question is - How can I replace content in the div id="content" with the one from another HTML page that can be accessed by clicking a link in the nav menu?

Reinstate Monica Cellio

Change the code that loads the navigation to this...

$("#mainhead").load("templates/header.html", function() {
    $(this).find(".nav a").on("click", function(e) {
        e.preventDefault();             // stop the link from firing as normal
        $("#content").load(this.href);  // load the file specified by the link
    });
});

This will "ajaxify" the links in the header navigation, after it has loaded, so that the content div is loaded for each of the links, without having to navigate away from the page. Just make sure each header link has the correct href value for the content you want it to load.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Replace anchor link with specific content using regex

分類Dev

Replace content that have newlines using sed

分類Dev

Replace div with input field text jQuery

分類Dev

Jquery draggable, check div content after drag

分類Dev

Jquery show/hide div on dynamic content

分類Dev

Expand div based on content using CSS?

分類Dev

Display a div if another div is hovered using Jquery

分類Dev

How to append the div before the div using jquery?

分類Dev

How to replace text in <label> using jquery

分類Dev

How replace Fields in Word document with their content using VBA

分類Dev

Replace the placeholder - the tilde ~ with the actual content using regular expression and sed

分類Dev

Edit web.config using get-content and replace in powershell

分類Dev

Jquery - How can I update a div with new content

分類Dev

.focus() - how to put the curser at end of content using contentEditable div

分類Dev

grab a div value using php curl or file_get_content

分類Dev

onhover div should be appear using jquery

分類Dev

Scrolling webpage using jquery onclick to a div

分類Dev

How to convert a 'div' into a dropdown using CSS/Jquery

分類Dev

Slide up, and hide a div using jquery on click

分類Dev

remove existing div and append to new div on load using jquery

分類Dev

How to replace all <br /> with \r\n using jQuery

分類Dev

How to remove inline styling of copied content from webpage using jQuery?

分類Dev

Background should adapt to content height using JQuery on window resizing

分類Dev

Test if table cell content is text or textarea using jquery

分類Dev

Jquery : Cannot add content with before() on a cloned block of code using prototype

分類Dev

Get HTML Content without comment lines using jquery

分類Dev

Create a word template and replace content control text with code using Open XML

分類Dev

div width property with content

分類Dev

Onclick to get another div data value using Jquery

Related 関連記事

  1. 1

    Replace anchor link with specific content using regex

  2. 2

    Replace content that have newlines using sed

  3. 3

    Replace div with input field text jQuery

  4. 4

    Jquery draggable, check div content after drag

  5. 5

    Jquery show/hide div on dynamic content

  6. 6

    Expand div based on content using CSS?

  7. 7

    Display a div if another div is hovered using Jquery

  8. 8

    How to append the div before the div using jquery?

  9. 9

    How to replace text in <label> using jquery

  10. 10

    How replace Fields in Word document with their content using VBA

  11. 11

    Replace the placeholder - the tilde ~ with the actual content using regular expression and sed

  12. 12

    Edit web.config using get-content and replace in powershell

  13. 13

    Jquery - How can I update a div with new content

  14. 14

    .focus() - how to put the curser at end of content using contentEditable div

  15. 15

    grab a div value using php curl or file_get_content

  16. 16

    onhover div should be appear using jquery

  17. 17

    Scrolling webpage using jquery onclick to a div

  18. 18

    How to convert a 'div' into a dropdown using CSS/Jquery

  19. 19

    Slide up, and hide a div using jquery on click

  20. 20

    remove existing div and append to new div on load using jquery

  21. 21

    How to replace all <br /> with \r\n using jQuery

  22. 22

    How to remove inline styling of copied content from webpage using jQuery?

  23. 23

    Background should adapt to content height using JQuery on window resizing

  24. 24

    Test if table cell content is text or textarea using jquery

  25. 25

    Jquery : Cannot add content with before() on a cloned block of code using prototype

  26. 26

    Get HTML Content without comment lines using jquery

  27. 27

    Create a word template and replace content control text with code using Open XML

  28. 28

    div width property with content

  29. 29

    Onclick to get another div data value using Jquery

ホットタグ

アーカイブ