我想循环遍历数组中包含的对象并更改每个对象的属性.如果我这样做:
for (var j = 0; j < myArray.length; j++){
console.log(myArray[j]);
}
Run Code Online (Sandbox Code Playgroud)
控制台应该调出阵列中的每个对象,对吧?但实际上它只显示第一个对象.如果我控制台将数组记录在循环外部,那么所有对象都会出现,所以肯定会有更多.
无论如何,这是下一个问题.如何使用循环访问数组中的Object1.x?
for (var j = 0; j < myArray.length; j++){
console.log(myArray[j.x]);
}
Run Code Online (Sandbox Code Playgroud)
这将返回"未定义".循环外的控制台日志再次告诉我,对象都具有"x"的值.如何在循环中访问这些属性?
我被推荐到其他地方为每个属性使用单独的数组,但我想确保我已经用尽了这个大道.
谢谢!
如果将this.style.background="#000";inline放入div中,例如onclick="this.style.background="#000";,它会起作用。但是,如果我将其放在函数中并从相同的onclick事件中调用该函数,则它将无法正常工作。但是,如果我使该函数执行其他操作(例如弹出一个警告框),则它确实可以工作。这是怎么回事?
这是代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<style>
.tile {
width: 48px;
height: 48px;
margin: 0px;
padding: 0px;
float: left;
background-color:red;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="tile" onclick="myFunction()"></div>
<script>
function myFunction() {
this.style.background="#000000";
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我从下面的代码中得到了丢失的错误.JS Hint说a)预计来自顶线,但"如果"发现了.
denom = (((theZombies[j].y + 15) – theZombies[j].y) * (mouseX – Player1.x) – ((theZombies[j].x + 18) – (theZombies[j].x - 18 )) * (mouseY - Player1.y));
if (denom != 0) {theZombies.splice(j, 1);}
Run Code Online (Sandbox Code Playgroud)
但是我没理解 - 所有的括号都被正确关闭了.我也回顾了代码,之前找不到一个未封闭的代码.我缺少什么想法?
谢谢!