这段代码在 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>
谢谢 :)