document.write有时会起作用,有时候不起作用

时间:2014-12-12 13:30:00

标签: if-statement innerhtml document.write

我在这里做错了什么?最后一位(kkk或mmm)不起作用。我该如何修改我的代码?

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body bgcolor="#E6E6FA">
        <input type="text" id="userInput"></input>
        <button onclick="test()">Submit</button>
        <p id="Demo"></p>
        <p id="Beg"></p>
        <p id="Fin"></p>
        <script>
            function test()
            {
                var nam= document.getElementById("userInput").value;
                var l = nam.length;
                var sta = nam.slice(0,1);
                var las = nam.slice(-1);
                document.getElementById("Demo").innerHTML = document.write("Your secret code: " + l + sta + las);
                var one = nam.slice(-1)
                if (one == "a") {
                   document.write(nam.slice(0,-1) + "bbb");
                } else {
                   document.write(nam + "ccc"); 
                }         
                document.getElementById("Beg").innerHTML = document.write(". Your public code: ");
//This doesn't work:
                if (one == "l") {
                   document.write(nam.slice(0,-1) + "kkk");

                } else {
                   document.write(nam + "mmm"); 
                }


            }
        </script>
    </body>
    </html>

我已经阅读了类似的问题,但我仍然不确定该怎么做,所以我非常感谢您的帮助!

编辑:字符串&#34;。您的公共代码:&#34;显示。

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

document.getElementById("Beg").innerHTML = document.write(". Your public code: ");

document.write不返回值。你应该使用istead

document.getElementById("Beg").innerHTML = ". Your public code: ";

希望有所帮助。