我找不到我的代码有什么问题

时间:2015-10-19 04:47:57

标签: javascript html

我无法找到为什么此代码中div中的第一个按钮不能用于HTML文件。 我检查了很多javaScript和HTML验证器,但没有一个工作。 在codecademy.com和w3schools.com上它可以工作,但不能在HTML文件上工作。

<!DOCTYPE html>
<html>
 <head>
  <style>
   #b1 {
        background-image: url('http://images2.fanpop.com/image/photos/14600000/egypt-egyptian-history-14635051-430-287.jpg');
        background-repeat: no-repeat;
        background-size: cover;
    }

    div {
        width: 990px;
        padding: 25px;
        border: 2px solid navy;
        margin: 25px;
    }
</style>
</head>
<body id="b1">
 <script>
    var b = 0;
    var a = function () {
        var q = "blah"
        document.getElementById('r').innerHTML = q;
    };

    var g = function () {
        location.reload();
    };
    var ab = function () {
        var p = "blah";
        document.getElementById('r').innerHTML = p;
    };
    var aq = function () {
        var an = "blah";
        document.getElementById('r').innerHTML = t;
    };
    var di = function () {
        var pe = "blah";
        document.getElementById('r').innerHTML = pe;
    };
    var e = true;
    var a = function () {
        if (e === true) {
            e = false;
            document.getElementById('a').innerHTML = "Ancient Egypt";
        }
        else {
            e = true;
            document.getElementById('a').innerHTML = "Erik Low\'s";
        }

    }
    setInterval(function () { a(); }, 4000);
</script><center>
    <div style="background:white" id="r">

        <h1>Ancient Egypt</h1>
        <h2>Achievements</h2>
        <hr />
        <button onclick="a();">Pyramids</button><br /><br />
        <button onclick="ab();">The Sphinx</button><br /><br />
        <button onclick="aq();">Writing</button><br /><br />
        <button onclick="v();">Mummification</button><br /><br />
        <button onclick="di();">Valley Of The Kings</button><br /><br />
    </div>
</center>

2 个答案:

答案 0 :(得分:0)

你错过了一个;

var q= "blah";

另外

document.getElementById('a')

这是null,因此没有innerHTML为null。 您没有id =&#39; a&#39;

的html标记

答案 1 :(得分:-1)

t变量,v函数,JS代码中缺少分号。

 <!DOCTYPE html>
    <html>
    <head>
    <style> #b1{
    background-image: url('http://images2.fanpop.com/image/photos/14600000/egypt-egyptian-history-14635051-430-287.jpg');
    background-repeat:no-repeat;

    background-size:cover;
    }
    div {
    width: 990px;
    padding: 25px;
    border: 2px solid navy;
    margin: 25px;
    }




    </style>
    </head>
    <body id="b1">
    <script>
    var b = 0;
    var a = function() {
    var q= "blah";
    document.getElementById('r').innerHTML = q; 
    };

    var g = function(){
    location.reload();
    };

    var v=function(){
        alert("function v calling");
    }
    var ab=function(){
        var p = "blah";
        document.getElementById('r').innerHTML = p;
         };
         var aq = function(){
        var an = "blah";  
        var t="writing";
     document.getElementById('r').innerHTML = t;
             };
             var di = function(){
                 var pe = "<h1>Valley Of The Kings</h1><hr/><br/><img src=\"http://3.bp.blogspot.com/-YUOUb6HSFcg/UbjX528O3zI/AAAAAAAAFw4/BXgpfsGvHgw/s1600/t1.jpg\" width=\"700\"><br/><h2>What Is The Valley Of The Kings</h2><p>The Valley Of The Kings is a valley in which many pharaohs were burried.</p><br/><h2>Who Was Burried There</h2><br/><p>In the East Valley Of The Kings Ramesses VII,<br/> Ramesses IV, Ramesses XI, Ramesses IX, Ramesses II,<br/> Merenptah, Ramesses V, Ramesses VI, Amenmesse,<br/> Ramesses III, Ramesse Khamenteru, Amenherkhepshef,<br/> Mentuherkhepshef, Twosret, Setnakhte, Seti II, Ramesses I, Seti I, Ramesses X, Mentuherkhepshef,<br/> Thutmose I, Hatshepsut, Tia\'a, Thutmose III, Amenhotep II, Maiherpri,<br/> Amenhotep I, Hatshepsut-Meryetre, Thutmose IV, Userhet, Yuya,<br/> Tjuyu, Siptah, Amenemopet called Pairy, Akhenaten, Horemheb,<br/> Sitre In, and Tutankhamen were burried.<br/> In the West valley Ay, Akhenaten, and Amenhotep III were burried.</p><br/><br/><p>Sources</p>";
                 document.getElementById('r').innerHTML = pe;
                 };
                 var e=true;
    var a = function() {
    if (e===true){
    e=false;
    document.getElementById('r').innerHTML = "Ancient Egypt";
    }
    else{
    e=true;
    document.getElementById('r').innerHTML = "Erik Low\'s";
    }

    }
    setInterval(function(){ a(); }, 4000);
    </script><center>
    <div style="background:white" id="r">

    <h1>Ancient Egypt</h1>
    <h2>Achievements</h2>
    <hr/>
    <button onclick="a();">Pyramids</button><br/><br/>
    <button onclick="ab();">The Sphinx</button><br/><br/>
    <button onclick="aq();">Writing</button><br/><br/>
    <button onclick="v();">Mummification</button><br/><br/>
    <button onclick="di();">Valley Of The Kings</button><br/><br/>
    </div>
    </center>

    </body>
    </html>