如何在不重新正确重新加载整个页面的情况下更改网页的一部分?

时间:2012-05-29 20:04:04

标签: php javascript jquery html ajax

我尝试调用javascript函数使用导航栏更改php页面,而无需重写整个页面,但我已经进入的关闭是我用一个按钮命令但javascript里面的内容完成了页面被关闭了,我做错了什么?

<script src="js/jquery.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery-1.6.2.min.js"></script>

 <script>
$(document).ready(function() {
$("#accordion").accordion();
$('#button_1, #button_2, #button_3, #button_4').click(function(){
                var which_to_load = ( $(this).attr('id')+"" ).replace('button_', ''),
                source_of_file = 'content'+which_to_load+'.php';
                $('#content_box').load( source_of_file );
                return false;
            });
});
     </script>

链接部分:

<div id="content_changing_buttons">
     <form method="get" id="change_content_buttons" action="">
       <p>
        <li><input value="1" type="submit" id="button_1" name="page" /> Reggiani presents at frankfurt Light + Build 2012</a><br/><br /></li>
        <li><input value="2" type="submit" id="button_2" name="page" /> Reggiani presents at frankfurt Light + Build 2012</a><br/><br /></li>
        <li><input value="3" type="submit" id="button_3" name="page" /> Reggiani presents at frankfurt Light + Build 2012<br/><br /></li>
        <li><input value="4" type="submit" id="button_4" name="page" /> Reggiani presents at frankfurt Light + Build 2012<br/><br /></li>
       </p>
    </form>
    </div>

内容部分:

     

  // default number of page to load
        $defaultPageNumber = '2';
        // check if content number is set
        $numberOfPageToInclude = isset($_GET['page']) ? $_GET['page'] : $defaultPageNumber;

        $options = array(
            'options' => array(
                'min_range' => 1,
                'max_range' => 4,
                ));

        if (filter_var($numberOfPageToInclude, FILTER_VALIDATE_INT, $options) !== FALSE) {
            include ('content' . $numberOfPageToInclude . '.php');
        } else {
            echo '<span style="color:red;">Wrong page number, including default page!</span><br />';
            include 'content1.php';
        }
        ?>
 </div>

现在我已经完成了它的建议,但是当我调用代码时,首先显示默认的php页面,然后当我单击按钮更改页面的php端时,它工作但不知何故它关闭了其余的javascript,甚至我的手风琴js和blinds js(它从另一个php页面引用)突然关闭。

任何人都可以建议如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

          <html>
          <head>
          <script language="javascript" type="text/javascript">
          function showentry()
             {
             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("right").innerHTML=xmlhttp.responseText;
                   }
                }
            xmlhttp.open("GET","details.php",true);//whatever the contents of file                                       details.php displayed in right div
            xmlhttp.send();
            }
             </script>
             <style type="text/css">
                #left{width:500px;height:100%; float:left;}
                #right{width:500px; float:left;}
             </style>
           </head>
       <div id="left">
      <input type="button" name="GET" value="show" onclick="showentry()"/>
        </div>
        <div id="right">
        </div>
        </html>