jquery ajax load certain div

后端 未结 3 633
耶瑟儿~
耶瑟儿~ 2020-12-03 05:52

my question is very simple how can I retrieve certain div with jquery ajax comand

$.ajax({
      url: \"test.html\",
      success: function(){
           $         


        
相关标签:
3条回答
  • 2020-12-03 06:00

    Here is a slightly different take on it. Also your target div maybe hidden by default so I've added in a Fade In affect to show it. If your html is going to change then you might want to add in a cache: false.

    $.ajax({
      url: "html.htm",
      type: "GET",
      dataType: "html",
      success: function (res) {
           $("#targetDiv").html($(res).find("#sourceDiv")
                                      .addClass('done'))
                         .fadeIn('slow');
      }
    });
    

    If you're interested you can take a look at how jQuery does the load method here jQuery Source Viewer

    0 讨论(0)
  • 2020-12-03 06:16

    If the div is part of the AJAX response:

    $.ajax({
        url: 'test.html',
        dataType: 'html',
        success: function(html) {
            var div = $('#sourceDiv', $(html)).addClass('done');
            $('#targetDiv').html(div);
        }
    });
    
    0 讨论(0)
  • 2020-12-03 06:26

    Here is an example that I use on my site for the navigation.

    <ul id="nav">        
        <li><a name="portfolio" href="portfolio.php" class="ajax_nav">PORTFOLIO</a></li>
        <li><a name="biography" href="biography.php" class="ajax_nav">BIOGRAPHY</a></li>
    </ul> 
    

    For the javascript you can try this.

    var hash = window.location.hash.substr(1);
    var hash2 = window.location.hash.substr(1);
    
    // Menu items to lower main content
    var href = $('.ajax_nav').each(function(){
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-4)){
            var toLoad = hash+'.html #ajax_content';
            $('#ajax_content').load(toLoad)
        }                                           
    });
    
    // For inner overlay of featured content. 
    var href2 = $('.feat_item_link').each(function(){
        var href2 = $(this).attr('href');
        if(hash2==href2.substr(0,href.length-4)){
            var toLoadFeatured = hash2+'.html #ajax_featured_content';
            $('#ajax_featured_content').load(toLoadFeatured);
        }                                           
    });
    
    // For Bottom Navigation
    $('#nav li a').click(function(){
    
        var toLoad = $(this).attr('href')+' #ajax_content';
        $('#content').hide('fast',loadContent);
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal');
    
        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href'));
        function loadContent() {
            $('#content').delay(1000).load(toLoad,'',showNewContent());
        }
        function showNewContent() {
            $('#content').show('normal',hideLoader());
        }
        function hideLoader() {
            $('#load').delay(1000).fadeOut('normal');
        }
        return false;
    });
    

    The id of #load simply uses an animated gif in the CSS.

    Place the javascript in the $(document).ready() and you should be all set.

    0 讨论(0)
提交回复
热议问题