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

标签 javascript html mysql css

更新:

您好,我创建了一个导航栏,可以将不同的页面加载到一个 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/

相关文章:

javascript - 使用自定义 ng 鼠标进入/离开摆脱奇怪的行为

html - Angular1 : TypeError: promise. catch 不是函数

javascript - 旋转和不透明度导致 Firefox 中的文本呈现问题

javascript - 添加和查找动态添加到主体的类 - jquery

php - 如何在 Zend Framework 中使用 GROUP_CONCAT?

mysql - 连接到 docker-compose mysql 容器会拒绝访问,但运行相同镜像的 docker 不会

javascript - 是否可以找到具有特定开头的 html 属性? (没有具体数值)

javascript - 如何在 ReactJs 中对数组数据进行排序

javascript - 如何让带有 false animate 队列的 onClick 函数也运行外部函数?

php - Drupal 7 hook_form_alter 和提交表单