dongwu5743 2015-09-15 20:56
浏览 37
已采纳

javascript数组返回[object Window]而不是数组内容

I have a problem. I wrote some PHP code that echoes "var pixelArray = [];". after that it echoes a few variables like this: "var canvas$id = 'can' + $id;". and also echoes "pixelArray.push(canvas$id);" for each variable.

Now for some reason when i run the script, the array doesn't contain anything.

Can anyone explain to me why?

If you need more information, please tell me.

EDIT:

Here's my full (but still simplyfied) code:

<?php
        $blockCounter = 1;
        $scriptCounter = 1;
        $arrayCounter = 1;
        $size = 32 * 32;
        $yLine = 1;
        while ($blockCounter <= $size)
        {

            if ($blockCounter == 32 * $yLine){
                echo "<canvas id=\"can$blockCounter\" width=\"17\" height=\"17\" style='border:1px solid #777777;'>GET A NEW BROWSER</canvas>";
                echo "<br>";
                $blockCounter = $blockCounter + 1;
                $yLine = $yLine + 1;
            }
            else{
                echo "<canvas id=\"can$blockCounter\" width=\"17\" height=\"17\" style='border:1px solid #777777;'>GET A NEW BROWSER</canvas>";
                $blockCounter = $blockCounter + 1;
            }

        }
        echo "<script>";
        echo "var pixelArray = [];";
        while ($arrayCounter <= $size)
        {
            echo "var name$arrayCounter = 'can' + $arrayCounter;";
            echo "pixelArray.push(name$arrayCounter);";
            $arrayCounter = $arrayCounter + 1;
        }
        while ($scriptCounter <= $size)
        {
            echo "var c = document.getElementById(\"can$scriptCounter\");";
            echo "picoco$scriptCounter = c.style.backgroundColor = \"#000000\";";
            $scriptCounter = $scriptCounter + 1;
        }
        echo "document.getElementById(\"arrayLoc\").innerHTML = pixelArray[0];";
        echo "</script>";
    ?> 
    <p id="arrayLoc"></p>
  • 写回答

1条回答 默认 最新

  • douxi0098 2015-09-16 04:00
    关注

    document.getElementById("arrayLoc") is returning null, not the pixelArray[0]. The problem is that the paragraph tag has not been loaded into the DOM at the time of the javascript load. You can put it above the script shown below.

        echo '<p id="arrayLoc"></p>';
        echo "<script>"
        echo "var pixelArray = [];";
        while ($arrayCounter <= $size)
        {
            echo "var name$arrayCounter = 'can' + $arrayCounter;";
            echo "pixelArray.push(name$arrayCounter);";
            $arrayCounter = $arrayCounter + 1;
        }
        while ($scriptCounter <= $size)
        {
            echo "var c = document.getElementById(\"can$scriptCounter\");";
            echo "picoco$scriptCounter = c.style.backgroundColor = \"#000000\";";
            $scriptCounter = $scriptCounter + 1;
        }
        echo "document.getElementById(\"arrayLoc\").innerHTML = pixelArray[0];";
        echo "</script>";
    ?> 
    

    Or run the javascript after the DOM has loaded.

        echo "<script>";
        echo "window.onload = function () {";
        echo "var pixelArray = [];";
        while ($arrayCounter <= $size)
        {
            echo "var name$arrayCounter = 'can' + $arrayCounter;";
            echo "pixelArray.push(name$arrayCounter);";
            $arrayCounter = $arrayCounter + 1;
        }
        while ($scriptCounter <= $size)
        {
            echo "var c = document.getElementById(\"can$scriptCounter\");";
            echo "picoco$scriptCounter = c.style.backgroundColor = \"#000000\";";
            $scriptCounter = $scriptCounter + 1;
        }
        echo "document.getElementById(\"arrayLoc\").innerHTML = pixelArray[0];";
        echo "}";
        echo "</script>";
    ?> 
    <p id="arrayLoc"></p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图