Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript c9中未定义文档_Javascript_Html_Dom_Domdocument_Referenceerror - Fatal编程技术网

Javascript c9中未定义文档

Javascript c9中未定义文档,javascript,html,dom,domdocument,referenceerror,Javascript,Html,Dom,Domdocument,Referenceerror,每当我运行代码时,它都会给我以下错误消息: ReferenceError: document is not defined at Object.<anonymous> (/home/ubuntu/workspace/random.js:1:63) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load

每当我运行代码时,它都会给我以下错误消息:

ReferenceError: document is not defined
    at Object.<anonymous> (/home/ubuntu/workspace/random.js:1:63)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:990:3


Process exited with code: 1
random.js

document.getElementById("randomButton").onclick = randomize();

var Elements = [
    document.getElementById("container"),
    document.getElementById("navi"),
    document.getElementById("randomButton")
];
var red = 0;
var green = 0;
var blue = 0;

function randomize() {
    console.log("Button pressed");
    red = randomValue(255);
    green = randomValue(255);
    blue = randomValue(255);
    for(var i = 0; i < Elements.length; i++) {
        //console.log("here");
        Elements.style.backgroundColor = "red";//"rgb(red, green, blue)";
        //Does not actually randomize anything yet
        //I want to get the button to at least do something before I move onto the next step
    }
}

function randomValue(max) {
    return Math.floor(Math.random() * max);
}
document.getElementById(“随机化按钮”).onclick=randomize();
变量元素=[
document.getElementById(“容器”),
document.getElementById(“navi”),
document.getElementById(“随机按钮”)
];
var-red=0;
绿色变量=0;
var蓝=0;
函数随机化(){
控制台日志(“按下按钮”);
红色=随机值(255);
绿色=随机值(255);
蓝色=随机值(255);
对于(var i=0;i

编辑:我在c9.io中这样做,以防相关。

使用
。onclick=randomize
而不是
。onclick=randomize()我按照您的建议进行了更改,但未定义的文档仍然是另一个问题。似乎此错误来自Node.js控制台,而不是浏览器控制台。在Node.js中,您无法访问文档。假设c9使用Node.js,我该如何代替文档?
#container {
    background-color: #dddddd;
}
#navi {
    position: static;
    height: 50px;
    background-color: #7777ff;
}
document.getElementById("randomButton").onclick = randomize();

var Elements = [
    document.getElementById("container"),
    document.getElementById("navi"),
    document.getElementById("randomButton")
];
var red = 0;
var green = 0;
var blue = 0;

function randomize() {
    console.log("Button pressed");
    red = randomValue(255);
    green = randomValue(255);
    blue = randomValue(255);
    for(var i = 0; i < Elements.length; i++) {
        //console.log("here");
        Elements.style.backgroundColor = "red";//"rgb(red, green, blue)";
        //Does not actually randomize anything yet
        //I want to get the button to at least do something before I move onto the next step
    }
}

function randomValue(max) {
    return Math.floor(Math.random() * max);
}