gpt4 book ai didi

javascript - 放置 JS 后导航器停止工作

转载 作者:行者123 更新时间:2023-11-29 04:45:55 25 4
gpt4 key购买 nike

更新:

您好,我创建了一个导航栏,可以将不同的页面加载到一个 div 中。这是导航栏的来源:

<div id="navBar">
<ul>
<li><a href="#" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Home</a></li>
<li><a href="about.php" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Staff</a></li>
<li><a href="goodies.php" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Goodies</a></li>
<li><a href="stuff.php" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Events</a></li>
<li><a href="#/contact.php" onClick="document.getElementById('bottom').innerHTML = loadPage('load.php');">News</a></li>
<li><a href="#" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Games Center</a></li>
<li><a href="phptesting.php" onClick="document.getElementById('bottom').innerHTML = loadPage('hello-world.html');">Habbo</a></li>
</ul>

这是放置文本的 div:

    <!-- Main Body -->
<div id="right">
<a id="bottom">
</div></a>

然后这是 javascript 女巫获取文件并将其放入 div 中:

function loadPage(href)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", href, false);
xmlhttp.send();
return xmlhttp.responseText;
}

现在,这两个代码的作用是当其中一个文本(例如“主页”)被点击时,在底部 Div 之间加载文本。当我将此代码放在此处以在页面加载时加载文本时:

<script>
function loadXMLDoc(filename) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
after_load_function(xmlhttp.responseText);
}
}
xmlhttp.open("GET",filename,true);
xmlhttp.send();
}

function after_load_function(responseText) {
document.getElementById("right").innerHTML = responseText;
}

window.onload = function() {
loadXMLDoc("welcome.html");
}
</script>

当我放置它时,它会导致页面不再加载。但是,如果我删除它,它就会开始工作,有什么想法吗?

最佳答案

为什么不使用 jQuery 使其支持跨浏览器呢?原生 js 很好,但可能不适用于跨浏览器。尝试使用 jQuery 看看您的问题是否可以解决。

关于javascript - 放置 JS 后导航器停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18997691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com