【问题标题】:Load external page into DIV将外部页面加载到 DIV
【发布时间】:2012-12-27 14:45:56
【问题描述】:

此代码在 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>  

谢谢:)

【问题讨论】:

    标签: jquery html jquery-load


    【解决方案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() function

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2011-12-30
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多