상자 안에 임의의 위치에 X를 배치 할 값을 생성 할 수 없습니다. 어떤 생각이 잘못 됐습니까?Math.random not working
var putAnXBtn = document.getElementById("putAnXButton");
putAnXBtn.onclick = function(){
var theBox = document.getElementById("putAnX");
// width and height of the box
var width = theBox.clientWidth;
var height = theBox.clientHeight;
var yPosition = Math.random()* width; // Not able to generate a value between 0 and the height
var xPosition = Math.random()* height; //Not able to generate a value between 0 and the width
var theXElement = document.getElementById("theX");
theXElement.innerHTML="X";
theXElement.style.top = parseInt(yPosition)+'px';
theXElement.style.left = parseInt(xPosition)+'px';
}
당신은뿐만 아니라 당신의 HTML을 보여 주실 래요? –
'width'와'height' 검사가 예상대로입니다 –
'theX'에는 위치 설정이 없으므로 위쪽과 왼쪽 값을 설정해도 이동하지 않습니다. CSS에서'theX'에 대한 상대적인 위치를 설정하면됩니다. – adeneo