Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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:函数参数未定义错误_Javascript_Html_Css_Function - Fatal编程技术网

Javascript:函数参数未定义错误

Javascript:函数参数未定义错误,javascript,html,css,function,Javascript,Html,Css,Function,使用脚本在任意网页上用箭头键随机移动正方形。我遇到一个错误,说“e”不存在 var page = document.getElementsByTagName("body")[0]; var bruhmoment = document.createElement("div"); bruhmoment.style.height = "100px"; bruhmoment.style.width = "100px"; br

使用脚本在任意网页上用箭头键随机移动正方形。我遇到一个错误,说“e”不存在

var page = document.getElementsByTagName("body")[0];
var bruhmoment = document.createElement("div");
bruhmoment.style.height = "100px";
bruhmoment.style.width = "100px";
bruhmoment.style.display = "block";
function getColor(){
    var color = "#";
    for(var i = 0; i < 6; i++){
        color += ( Math.round(Math.random()) * 9 );
    }
    return color;
}
bruhmoment.style.backgroundColor = getColor();
bruhmoment.style.zindex = 1e9;
bruhmoment.id = "bruhmoment";
bruhmoment.style.marginLeft = "0px";
bruhmoment.style.marginBottom = "100px";
bruhmoment.xaxis = 0;
bruhmoment.yaxis = 0;
function move(e){
    switch(e.which){
        case 39:
            xaxis += 10;
            bruhmoment.style.marginLeft = xaxis + "px";
            break;
        case 37:
            xaxis -= 10;
            bruhmoment.style.marginLeft = xaxis + "px";
            break;
        case 38:
            yaxis += 10;
            bruhmoment.style.marginBottom = yaxis + "px";
            break;
        case 40:
            xaxis -= 10;
            bruhmoment.style.marginBottom = yaxis + "px";
            break;
        case 67:
            bruhmoment.style.backgroundColor = getColor();
            break;
    }
}
page.onkeydown = move(event);   
page.appendChild(bruhmoment);
var page=document.getElementsByTagName(“body”)[0];
var bruhmoment=document.createElement(“div”);
bruhmoment.style.height=“100px”;
bruhmoment.style.width=“100px”;
bruhmoment.style.display=“block”;
函数getColor(){
var color=“#”;
对于(变量i=0;i<6;i++){
颜色+=(Math.round(Math.random())*9);
}
返回颜色;
}
bruhmoment.style.backgroundColor=getColor();
bruhmoment.style.zindex=1e9;
bruhmoment.id=“bruhmoment”;
bruhmoment.style.marginLeft=“0px”;
bruhmoment.style.marginBottom=“100px”;
bruhmoment.xaxis=0;
bruhmoment.yaxis=0;
功能移动(e){
开关(e.which){
案例39:
xaxis+=10;
bruhmoment.style.marginLeft=xaxis+“px”;
打破
案例37:
xaxis-=10;
bruhmoment.style.marginLeft=xaxis+“px”;
打破
案例38:
yaxis+=10;
bruhmoment.style.marginBottom=yaxis+“px”;
打破
案例40:
xaxis-=10;
bruhmoment.style.marginBottom=yaxis+“px”;
打破
案例67:
bruhmoment.style.backgroundColor=getColor();
打破
}
}
page.onkeydown=移动(事件);
第页:附加子项(bruhmoment);

在这段代码中所有可能是错误的东西中,我不确定JavaScript是如何选择要与之争吵的特定错误的。有人能告诉我吗?

page.onkeydown=move(事件)应该是
page.onkeydown=move
将onkeydown处理程序设置为move函数

而且

应该是公正的

xaxis = 0;
yaxis = 0;
因为这就是它们在脚本其余部分中的使用方式,所以DOM元素无论如何都没有这些属性

案例40:
之后还有一个打字错误-应该是yaxis而不是xaxis

马金托普,不是马金托姆


yaxis+=/-=应该被交换。

它应该是
page.onkeydown=move
函数移动(evt){}
。请注意,在此之后,您必须声明
e=event

此外,您的下箭头键正在更改
xaxis
而不是
yaxis
,您没有声明
xaxis
yaxis
-您输入了
bruhmoment.xaxis
bruhmoment.yaxis
,并且您正在使用上下键更改
marginBottom
,而不是
marginTop
。但是,我修正了:)

var page=document.getElementsByTagName(“body”)[0];
var bruhmoment=document.createElement(“div”);
bruhmoment.style.height=“100px”;
bruhmoment.style.width=“100px”;
bruhmoment.style.display=“block”;
函数getColor(){
var color=“#”;
对于(变量i=0;i<6;i++){
颜色+=(Math.round(Math.random())*9);
}
返回颜色;
}
bruhmoment.style.backgroundColor=getColor();
bruhmoment.style.zindex=1e9;
bruhmoment.id=“bruhmoment”;
bruhmoment.style.marginLeft=“0px”;
bruhmoment.style.marginBottom=“100px”;
var-xaxis=0;
var-yaxis=0;
功能移动(evt){
e=事件
开关(e.which){
案例37://左
xaxis-=10;
bruhmoment.style.marginLeft=xaxis+“px”;
打破
案例38://以上
yaxis-=10;
bruhmoment.style.marginTop=yaxis+“px”;
打破
案例39://对
xaxis+=10;
bruhmoment.style.marginLeft=xaxis+“px”;
打破
案例40://向下
yaxis+=10;
bruhmoment.style.marginTop=yaxis+“px”;
打破
案例67://颜色变化
bruhmoment.style.backgroundColor=getColor();
打破
}
}
page.onkeydown=移动;

第页:附加子项(bruhmoment)应该是
page.onkeydown=move
函数移动(evt){}
。请注意,在这之后,您必须声明
e=event
。您还出现了一些小的打字错误-通过一个工作演示修复了所有这些问题:
xaxis = 0;
yaxis = 0;