2012-03-13 43 views
0

这个问题让我疯狂......我通过Ajax调用一个PHP文件并将其解析为我的HTML内容。我必须这样做才能拥有动态内容,而不必刷新页面。JQuery不支持解析的内容

我的问题是,解析的代码将不会执行任何jQuery的东西。例如在PHP文件中需要一个Jquery滑块。但它被解析为普通的HTML,JQuery似乎并不认识它。

这里有一个例子:

HTML文件:

<html> 
    <head> 
     <script src="jquery-1.7.1" type="text/javascript"></script> 
     <script src="jscroller-0.4.js" type="text/javascript"></script> 

     <script type="text/javascript"> 
      $(document).ready(function() 
      { 
       // Add Scroller Object 
       $jScroller.add("#scroller_container","#scroller","right",1); 
       // Start Autoscroller 
       $jScroller.start(); 
      }); 
     </script> 


     <script type="text/javascript"> 
      function showGet(str) 
      { 
       if (str=="") 
       { 
       document.getElementById("center").innerHTML=""; 
       return; 
      } 

      if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 
      } 
      else 
      {// code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 

      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("content").innerHTML=xmlhttp.responseText; 
       } 
      } 

      xmlhttp.open("GET","content.php?q="+str,true); 
      xmlhttp.send(); 
      } 
     </script> 
    </head> 

    <body> 
     <div id="content"> 
      <input type="button" onclick="showGet(1)" value="showGet"> 
     </div> 

     <div id="scroller_container"> 
     <div id="scroller"> 
      ...[Content]... 
     </div> 
     </div> 
    </body> 
</html> 

这里的content.PHP文件:

<?php 

    echo " 
     <div id=\"scroller_container\"> 
      <div id=\"scroller\"> 
       ...[Content]... 
      </div> 
     </div> 
     " 

?> 

执行时,HTML有一个按钮。如果您点击该按钮,它会将content.php解析为<div id="content></div>。 PHP包含与HTML相同的<div id="scroller" ....

HTML div的工作原理,不是PHP文件。为什么????我如何才能使它工作?

许多thx提前的所有提示!

+2

你为什么不使用jQuery的Ajax功能?他们实现起来要容易得多,另外他们还从内置的Ajax结果中执行脚本。 – 2012-03-13 21:31:54

+0

我想但我想我错过了所需的知识。你有一个简单的例子吗? – 2012-03-13 21:33:05

+1

该手册有一些例子:http://api.jquery.com/jQuery.ajax/ – 2012-03-13 21:34:00

回答

1

加载内容jQuery的AJAX可以简单的使用负载以下()方法:

http://api.jquery.com/load/

/* loads a file into id=content and replaces exisitng html*/ 
$('#content').load('path/to/server/file', function(){ 
    /* new content exists, intialize event handling 
    and plugins for new html here*/ 


}) 
+0

Wonderfull!那做了这个工作。首先要问你们。浪费了很多时间....非常感谢! – 2012-03-13 22:04:29

0

要到jQuery函数动态添加的信息访问,你必须使用委托()或() - 使用

http://api.jquery.com/on/ http://api.jquery.com/delegate/