Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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
AJAX:跳转到PHP使用xmlhttp.open动态创建的页面中的另一部分(onClick()是否在href之前运行?)_Php_Javascript_Css_Ajax_Xmlhttprequest - Fatal编程技术网

AJAX:跳转到PHP使用xmlhttp.open动态创建的页面中的另一部分(onClick()是否在href之前运行?)

AJAX:跳转到PHP使用xmlhttp.open动态创建的页面中的另一部分(onClick()是否在href之前运行?),php,javascript,css,ajax,xmlhttprequest,Php,Javascript,Css,Ajax,Xmlhttprequest,我想单击列表中的一个元素,它将填充另一个列表,并将我带到它;第二个列表需要使用AJAX和PHP从mySQL数据库加载。我想知道以下方法是否可行,或者是否存在更好的方法 HTML: PHP: 那么,PHP会在HTML中的href抓取节之前创建它吗?不会,因为Ajax是异步的。Ajax调用将被触发,但是loadContent()将在返回结果之前立即完成,因此它将尝试转到#/插入内容之前的第1节 我们可以做的是在Ajax完成后(在readyState部分中)移动到锚点,使用: <a onCl

我想单击列表中的一个元素,它将填充另一个列表,并将我带到它;第二个列表需要使用AJAX和PHP从mySQL数据库加载。我想知道以下方法是否可行,或者是否存在更好的方法

HTML:

PHP:



那么,PHP会在HTML中的href抓取节之前创建它吗?

不会,因为Ajax是异步的。Ajax调用将被触发,但是
loadContent()
将在返回结果之前立即完成,因此它将尝试转到
#/插入内容之前的第1节

我们可以做的是在Ajax完成后(在readyState部分中)移动到锚点,使用:

<a onClick=loadContent(this.value) value=Section1 href="#!/Section1">Section 1</a>

<li id=output></li>
function loadContent(section)
{
      .
      .
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
       document.getElementById("output").innerHTML=xmlhttp.responseText;
     }
xmlhttp.open("GET","getContent.php?q="+section,true);
xmlhttp.send()
}
<?php
   $q=$_GET["q"];

   //connecting and creating query etc.

   while($row = mysql_fetch_array($result))
   {
      print "\t" . '<li id=". $q ."><a href="#!/"'  . $row['subsection'] .'>' . $row['subsection'] . '</a></li>';
   }
   ?>
window.location.hash = '#!/Section1';