2016-01-20 104 views
0

我尝试加载ajax的php文件。jquery加载ajax后PHP不工作

这此代码工作正常:

<body id="top">  
<div id="loadajaxhere"></div> 
<script> 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      document.getElementById("loadajaxhere").innerHTML = xmlhttp.responseText; 
     } 
    }; 
    xmlhttp.open("GET", "myfile.php", true); 
    xmlhttp.send(); 
</script> 

但在我的PHP文件是jQuery插件,它通过AJAX加载后不工作... 也许解决办法是的jQuery语法使用AJAX。这样对吗?

我试过了,但我的Ajax没有加载php ... 它应该加载php自动加载页面在一个定义的div。

非常感谢提前!

+0

'myfile.php'的内容是什么? – AnkiiG

+0

是否有任何JQuery函数调用?请放置该代码。如果有的话,请检查您的浏览器控制台是否有JS错 –

+0

没有必要使用ajax加载任何javascript。只需将其添加到当前页面,如果有必要,在一些逻辑。例如http://stackoverflow.com/questions/15521343/conditionally-load-javascript-file –

回答

0
<script> 
     $.ajax({ 
       url: "myfile.php", 
       success: function(result){ 
        $("#loadajaxhere").html(result); 
       } 
     }); 
</script> 
+0

好吧,它正在加载,但它返回一个不同于非jQuery的函数。 –

+4

请解释你的答案。目前它被视为低质量的帖子。谢谢!加载它的 – Dropout

0

用jquery ajax代替。例如:

$.ajax({ 
     url: 'myfile.php', 
     type: 'GET', 
     data: {'submit':'true'}, // An object with the key 'submit' and value'true'; 
     success: function (result) { 
      document.getElementById("loadajaxhere").innerHTML = result; 
     } 
    }); 
+0

,但我的内容文件中的jQuery脚本不起作用。 –

0

一小部分解决

在myfile.php一个小脚本,现在的工作:JQuery effect doesnt work for ajax content

但是这个巨大的脚本:该解决方案的

<script type="text/javascript"> 
//$('.triggermore').click(function(){ //<- This is the old line 
$('body').on('click', '.triggermore', function(event){ //<- This is the new one 
     $(".weitere").slideDown(); 
     $(".weitere").addClass("open"); 
     $(".triggermore").addClass("bye"); 
     $('html, body').animate({ scrollTop: $("#weitere").offset().top }, 1000);  
}); 
</script> 

来源is stil not working:

<script type="text/javascript"> 
$(document).ready(function() { 

    //http://webdesign.tutsplus.com/tutorials/javascript-tutorials/create-a-sticky-navigation-header-using-jquery-waypoints/ 
    var nav_container = $(".menu"); 
    var nav = $("nav"); 

    var top_spacing = 0; 
    var waypoint_offset = '40%'; 
    var first_section = '0%'; 

    nav_container.waypoint({ 
     handler: function(event, direction) { 

      if (direction == 'down') { 
       nav_container.addClass("sticky") 
       .stop() 
       .css("top", -nav.outerHeight()) 
       .animate({"top" : top_spacing}); 
      } else {  
       var inputPos = $('input:first').position(); 
       nav_container.stop().removeClass("sticky").css("top",first_section).animate({"top":first_section}); 
      } 

     }, 
     offset: function() { 
      return -nav.outerHeight()-waypoint_offset; 
     } 

    }); 

    var sections = $("section"); 
    var navigation_links = $(".menu li a"); 
    var links = $("nav a"); 

    sections.waypoint({ 
     handler: function(event, direction) { 

      var active_section; 
      active_section = $(this); 
      if (direction === "up") active_section = active_section.prev(); 

      var active_link = $('.menu li a[href="#' + active_section.attr("class") + '"]'); 
      navigation_links.removeClass("selected"); 
      if(active_section.attr("class") != "top") { 
       active_link.addClass("selected"); 
      } 

     }, 
     offset: waypoint_offset 
    }) 


    links.click(function(event) { 

     $.scrollTo(
      $(this).attr("href"), 
      { 
       duration: 1500, 
       offset: { 'left':0, 'top':0 } 
      } 
     ); 
    }); 




}); 

</script> 

** Jquery脚本位于我的index.php而不是myfile.php中。 只有myfile.php中的html标记和

0

好的,我自己找到了答案。

进出口使用此:

$.ajax({ 
     url: 'myfile.php', 
     type: 'GET', 
     data: {'submit':'true'}, // An object with the key 'submit' and value'true'; 
     success: function (result) { 
      document.getElementById("loadajaxhere").innerHTML = result; 
     } 
    }); 

和粘贴我的更迭功能的脚本。但并非一切正常。 我在上面。