加载php文件文件与ajax onclick,没有jQuery

Load php file file with ajax onclick, without jQuery

本文关键字:文件 没有 onclick jQuery ajax php 加载      更新时间:2023-09-26

我正在努力寻找资源,其中显示了如何加载一个.php文件到一个页面上的div一旦某些按钮被点击。几乎所有的搜索结果都返回jQuery解决方案,而我首先想尝试使用标准JavaScript。

对于问题的目的,让我们说我想加载example.php#examplediv当用户点击#loadExample链接。

    <script>
function yourFunction() {
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("POST","yourpage.php",true);
  xmlhttp.send();
document.getElementById("yourdiv").innerHTML=xmlhttp.responseText;
}

</script>
<div onclick="yourFunction"></div> // or you can use button or hyper link
<div id="yourdiv"></div>