简单的 JS 代码不起作用
Simple JS code not working
我正在努力学习,所以如果你能指出错误而不实际告诉我那会很好,但如果它需要解决方案我也很好 - 我只是想要一个解释。
目标是单击按钮并将其移动到随机位置。我没有收到错误,但也没有收到 console.log 输出。如果我将 console.log 移出函数,我会得到输出。
var buttonState = document.getElementById("clickMe");
var maxWidth = screen.width;
var maxHeight = screen.height;
var screenWidth = 0;
var screenHeight = 0;
// Find max width and height of screen and set variables to random number within parameters
function moveButton() {
"use strict";
screenWidth = Math.floor(Math.random() * (maxWidth - 1 + 1)) + 1;
screenHeight = Math.floor(Math.random() * (maxHeight - 1 + 1)) + 1;
}
buttonState.onClick = function () {
"use strict";
movebutton();
buttonState.style.left = screenWidth.page + "px";
buttonState.style.top = screenHeight.page + "px";
console.log(screenWidth);
console.log(screenHeight);
};
buttonState.onClick
属性 名称是 onclick
(小写 c
)。 JavaScript 区分大小写。
我正在努力学习,所以如果你能指出错误而不实际告诉我那会很好,但如果它需要解决方案我也很好 - 我只是想要一个解释。
目标是单击按钮并将其移动到随机位置。我没有收到错误,但也没有收到 console.log 输出。如果我将 console.log 移出函数,我会得到输出。
var buttonState = document.getElementById("clickMe");
var maxWidth = screen.width;
var maxHeight = screen.height;
var screenWidth = 0;
var screenHeight = 0;
// Find max width and height of screen and set variables to random number within parameters
function moveButton() {
"use strict";
screenWidth = Math.floor(Math.random() * (maxWidth - 1 + 1)) + 1;
screenHeight = Math.floor(Math.random() * (maxHeight - 1 + 1)) + 1;
}
buttonState.onClick = function () {
"use strict";
movebutton();
buttonState.style.left = screenWidth.page + "px";
buttonState.style.top = screenHeight.page + "px";
console.log(screenWidth);
console.log(screenHeight);
};
buttonState.onClick
属性 名称是 onclick
(小写 c
)。 JavaScript 区分大小写。