Javascript 使用ajax在html页面中动态加载内容时出现语法错误
我试图在我的HTML div中动态加载一些内容。如果我尝试这样做,我会得到一个错误:语法错误主页。(这是应该在div中可见的内容) HTML: 导航栏:Javascript 使用ajax在html页面中动态加载内容时出现语法错误,javascript,html,ajax,xmlhttprequest,innerhtml,Javascript,Html,Ajax,Xmlhttprequest,Innerhtml,我试图在我的HTML div中动态加载一些内容。如果我尝试这样做,我会得到一个错误:语法错误主页。(这是应该在div中可见的内容) HTML: 导航栏: <li><a href="#" onclick="replaceContent('home.html')">Home</a></li> Javascript: function replaceContent(newDiv) { var xmlHttp = createXMLHttp();
<li><a href="#" onclick="replaceContent('home.html')">Home</a></li>
Javascript:
function replaceContent(newDiv) {
var xmlHttp = createXMLHttp();
xmlHttp.onreadystatechange = function(){replace(xmlHttp);}
xmlHttp.open("GET", newDiv, true);
xmlHttp.send(null);
}
function replace(xmlHttp) {
if (xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("pagepanel").innerHTML=xmlHttp.responseText;
}
alert(xmlHttp.responseText);
}
function createXMLHttp() {
var xmlHttp;
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}else{
alert("Please upgrade your browser! Your browser does not support AJAX!");
}
return xmlHttp;
}
由于缺少分号而发生错误
onclick="replaceContent('home.html')";>Home</a></li>
onclick=“replaceContent('home.html')”;>家
wat做这个'onLoad=“replaceDiv('home.html')什么都不做。对不起,我没有在那段html中重命名函数调用。我不太确定……但是你的home.html中不应该至少有一个(或其他)标记吗?我不知道,因为xmlHttp对象的open函数应该打开任何文件(例如txt、html、js)。我在这里尝试完成的唯一一件事是在div中设置文件的内容。包含html标记会破坏div的内部结构,因为div标记中会有html标记。(如果我错了,请纠正我)。为什么不使用jquery?这是一种要求?
function replaceContent(newDiv) {
var xmlHttp = createXMLHttp();
xmlHttp.onreadystatechange = function(){replace(xmlHttp);}
xmlHttp.open("GET", newDiv, true);
xmlHttp.send(null);
}
function replace(xmlHttp) {
if (xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("pagepanel").innerHTML=xmlHttp.responseText;
}
alert(xmlHttp.responseText);
}
function createXMLHttp() {
var xmlHttp;
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}else{
alert("Please upgrade your browser! Your browser does not support AJAX!");
}
return xmlHttp;
}
onclick="replaceContent('home.html')";>Home</a></li>