简体   繁体   English

如何使用变量中的javascript设置对象的宽度和位置?

[英]How do I set width and positioning of an object with javascript from a variable?

I am trying to create a button in javascript and make that button positioned below a canvas, the same width as the canvas, however, I don't seem to be able to set the top and width as a variable (the code is ignored). 我试图在javascript中创建一个按钮,并使该按钮位于画布下方,宽度与画布相同,但是,我似乎无法将top和width设置为变量(代码被忽略) 。 Here is my code: (problem lines commented) 这是我的代码:(问题行已注释)

var myButton = document.createElement('button');
myButton.style.position = 'absolute';
myButton.style.left = '0px';
myButton.style.top = canvasHeight; //
myButton.style.width = canvasWidth; //
myButton.style.height = '100px';
myButton.innerHTML = 'Restart!';
document.body.appendChild(myButton);

Note that the canvas is resizable, so I can't just type in a px value. 请注意,画布是可调整大小的,因此我不能只输入px值。

Here's my comment in an answer: 这是我在回答中的评论:

Make sure you have the correct format in canvasHeight and canvasWidth , eg "100px" rather than just "100". 确保您使用正确的canvasHeightcanvasWidth格式,例如“ 100px”,而不仅仅是“ 100”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM