因此,我希望做的是每半秒钟更改一组<p>标记内的文本。有问题的标签集在我体内的这段代码中:

<div class="outerdiv" id="col2">
    <p id="matrixText"></p>
</div>


在上述代码的正下方,我具有应每半秒调用一个函数的JavaScript:

<script type="text/javascript">
    setInterval("changeMatrixText()", 500);
</script>


我在脑海中定义了函数changeMatrixText

function changeMatrixText()
{
    var newtext = "";
    for (var i = 0; i < 1000; i++)
        newtext += Math.floor((Math.random()*10)+1) % 2 ? "0" : "1";
    document.getElementById("matrixText").value = newtext;
}


如您所见,这应该将文本设置为0和1的随机字符串。但这不起作用。知道为什么吗?

万一您需要查看我的整个代码...

<html>

<head>
    <title>Simple encrypt/decrypt</title>

    <style type="text/css">

        body
        {
            background-color: #A9F5F2;
            width: 900px;
            padding: 0px;
        }
        .outerdiv
        {
            margin: 5px;
            border: 2px solid #FF8000;
            background-color: #FFFFFF;
        }
        .outerdiv > p
        {
            margin: 5px;
            word-wrap:break-word
        }
        .outerdiv > h1
        {
            margin: 5px;
        }
        #col1
        {
            width: 500x;
            height: 800px;
            float: left;
        }
        #col2
        {
            width: 295px;
            height: 1500px;
            float: right;
            font-family: Courier New;
            overflow: hidden;
        }
        #title1div
        {
            font-family: Arial;
            width: 100%;
        }
        #insctdiv
        {
            font-family: Arial;
            width: 100%;
        }
        #iptdiv
        {
            height: 400px;
            width: 100%;
        }
        #buttonsdiv
        {
            text-align: center;
            width: 100%;
        }
        #inputText
        {
            width: 100%;
            height: 100%;
            resize: none;
        }

    </style>


    <script type="text/javascript">

        function encrypt()
        {
            var text = document.getElementById("inputText").value;
            newstring = "";
            /* Make newstring a string of the bit representations of
               the ASCII values of its thisCharacters in order.
            */
            for (var i = 0, j = text.length; i < j; i++)
            {
                bits = text.charCodeAt(i).toString(2);
                newstring += new Array(8-bits.length+1).join('0') + bits;
            }
            /* Compress newstring by taking each substring of 3, 4, ..., 9
               consecutive 1's or 0's and it by the number of such consecutive
               thisCharacters followed by the thisCharacter.
               EXAMPLES:
                    "10101000010111" --> "10101401031"
                    "001100011111111111111" --> "0011319151"
            */
            newstring = newstring.replace(/([01])\1{2,8}/g, function($0, $1) { return ($0.length + $1);});
            document.getElementById("inputText").value = newstring;
        }

        function decrypt()
        {
            var text = document.getElementById("inputText").value;
            text = text.trim();
            text.replace(/([2-9])([01])/g,
            function (all, replacementCount, bit) {
                return Array(+replacementCount + 1).join(bit);
            }).split(/(.{8})/g).reduce(function (str, byte) {
                return str + String.fromCharCode(parseInt(byte, 2));
            }, "");
            document.getElementById("inputText").value = text;
        }


        function changeMatrixText()
        {
            var newtext = "";
            for (var i = 0; i < 1000; i++)
                newtext += Math.floor((Math.random()*10)+1) % 2 ? "0" : "1";
            document.getElementById("matrixText").value = newtext;
        }

    </script>

</head>

<body>
    <div id="col1">
        <div class="outerdiv" id="title1div">
            <h1>Reversible text encryption algorithm</h1>
        </div>
        <div class="outerdiv" id="insctdiv">
            <p>Type in or paste text below, then click <b>Encrypt</b> or <b>Decrypt</b></p>
        </div>
        <div class="outerdiv" id="iptdiv">
            <textarea id="inputText" scrolling="yes"></textarea>
        </div>
        <div class="outerdiv" id="buttonsdiv">
            <button onclick="encrypt()"><b>Encrypt</b></button>
            <button onclick="decrypt()"><b>Decrypt</b></button>
        </div>
    </div>
    <div class="outerdiv" id="col2">
        <p id="matrixText"></p>
    </div>
    <script type="text/javascript">
        setInterval("changeMatrixText()", 500);
    </script>
</body>

</html>


本质上,我想让页面的右列继续每半秒在新的字符串0和1内打印一次,就像您在电影《黑客帝国》中的计算机屏幕上那样,如果您发现我的漂流情况。

最佳答案

根据MDN,具有值属性的元素包括<button><option><input><li><meter><progress><param>。您需要改为设置innerHTML

document.getElementById("matrixText").value = newtext;




document.getElementById("matrixText").innerHTML = newtext;




setInterval("changeMatrixText()", 500);




setInterval(changeMatrixText, 500);


Working Demo

09-28 05:17