1

这段代码在 IE 中运行良好,但在 Chrome 中不行。有人能指出我正确的方向,让它在大多数当前的浏览器中工作。

当您单击链接时,它应该将页面加载到 div 中。代码复制自: webdeveloper.com

<script>
function processAjax(url) {
      if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
      req.onreadystatechange = targetDiv;
      try {
        req.open("GET", url, true);
      } catch (e) {
        alert(e);
      }
      req.send(null);
    } else if (window.ActiveXObject) { // IE
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
        req.onreadystatechange = targetDiv;
        req.open("GET", url, true);
        req.send();

      }
    }
}

function targetDiv() {
    if (req.readyState == 4) { // Complete
          if (req.status == 200) { // OK response
              document.getElementById("MyDivName").innerHTML = req.responseText;
          } else {
            alert("Problem: " + req.statusText);
          }
    }
}  
</script>


<a href="javascript:processAjax('test.html');">CLICK ME</a>
<div id="myDivName"></div>  

谢谢 :)

4

1 回答 1

1

在 jQuery 中,你可以这样做:

$("#myDivName").load("test.html", function(response, status, xhr) {
   if (status == "error") {
      var msg = "Sorry but there was an error: ";
      alert(msg + xhr.status + " " + xhr.statusText);
   }
 });

看一看:jQuery API load() 函数

于 2012-12-30T07:07:28.853 回答