javascript - 使用 JavaScript 每半秒更改页面上的文本

标签 javascript html

所以,我希望做的是更改一组 <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 ,具有 value 属性的元素包括 <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

关于javascript - 使用 JavaScript 每半秒更改页面上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22519452/

相关文章:

php - 使用 ajax 将 javascript 变量传递给 php,结果不显示任何内容

php - 我如何使用 $_GET 从 index.php 中提取?23456

jquery - 淡出背景图像,然后在悬停时显示文本(不褪色)

html - 如何在导航菜单和页脚链接中隐藏 <li> 项目符号,但在列表项中显示它们

javascript - Highcharts 将 xAxis 下移/移出图表

javascript - 检查今天是否在日期数组中

javascript - 如何过滤 JQuery 对象?

当数字有 3 位小数并固定为 2 位小数时,Javascript number.toFixed() 不起作用

javascript - 如何在 Controller 文件中使用 socket.io

css - 努力在 div/背景问题中定位 div#