Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何实现这个Ajax脚本?_Javascript - Fatal编程技术网

Javascript 如何实现这个Ajax脚本?

Javascript 如何实现这个Ajax脚本?,javascript,Javascript,这是一个脚本,当点击一个链接时,它会从某个地方拖出一个页面 并将其插入当前页面的div中。很简单,是的,但是作为 我的脑袋似乎很笨,我不知道该怎么实现它 i、 e.我如何制定链接,使其将脚本指向页面 我想在我想要的div中加载 剧本: $(document).ready(function() { // Check for hash value in URL var hash = window.location.hash.substr(1); var href

这是一个脚本,当点击一个链接时,它会从某个地方拖出一个页面 并将其插入当前页面的div中。很简单,是的,但是作为 我的脑袋似乎很笨,我不知道该怎么实现它

i、 e.我如何制定链接,使其将脚本指向页面 我想在我想要的div中加载

剧本:

$(document).ready(function() {  

    // Check for hash value in URL  
    var hash = window.location.hash.substr(1);  
    var href = $('#nav li a').each(function(){  
        var href = $(this).attr('href');  
        if(hash==href.substr(0,href.length-5)){  
            var toLoad = hash+'.html #content';  
            $('#content').load(toLoad)  
        }   
    });  

    $('#nav li a').click(function(){  

    var toLoad = $(this).attr('href')+' #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').length-5);  
    function loadContent() {  
        $('#content').load(toLoad,'',showNewContent())  
    }  
    function showNewContent() {  
        $('#content').show('normal',hideLoader());  
    }  
    function hideLoader() {  
        $('#load').fadeOut('normal');  
    }  
    return false;

    });
});
  • 我们需要定义单击链接时从哪个页面获取数据:

    var toLoad = $(this).attr('href')+' #content';
    
  • loadContent函数调用请求的页面:

    function loadContent() {  
        $('#content').load(toLoad,'',showNewContent)  
    }
    
  • 很可能以上就是按预期运行脚本所需的全部内容 但前提是你知道怎么做,而我不知道


    PS:本教程的所有内容都是。

    基本上拦截所有链接点击,并发出AJAX请求。。。记住在click回调函数的末尾返回false

    $('a').click(function () {
      var href = $(this).attr('href');
      $.ajax({
        url: href,
        success: function (res) {
          $(res).appendTo('#target'); // add the requested HTML to #target
        }
      });
      return false; // important
    });
    

    基本上截获所有链接点击并发出AJAX请求。。。记住在click回调函数的末尾返回false

    $('a').click(function () {
      var href = $(this).attr('href');
      $.ajax({
        url: href,
        success: function (res) {
          $(res).appendTo('#target'); // add the requested HTML to #target
        }
      });
      return false; // important
    });