[英]How should I refer to a specific element in document.getComputedStyle?
I am making a game where there is a draggable object and I would like to store its actual place in real time. 我正在制作一个有可拖动对象的游戏,我想实时存储其实际位置。 I was told I should use getComputedStyle but it only works in case I use 有人告诉我我应该使用getComputedStyle,但是它只能在我使用的情况下起作用
var computedStyle = getComputedStyle(document.body, null)
I mean if I add 我的意思是如果我加上
document.write(computedStyle.marginTop)
it works and it writes the value of the top margin. 它可以正常工作,并写出上边距的值。 But I need a div element's place.. I tried using 但是我需要一个div元素的位置。
var place = document.getElementById("draggable");
var computedStyle = getComputedStyle(place, null);
document.write(computedStyle.marginTop);
but it does nothing. 但它什么也没做。 I also tried 我也试过
var computedStyle = getComputedStyle(document.getElementById("draggable"), null);
but it also did not work. 但它也没有用。
Can somebody please help me out how should I refer to the div element with the id "draggable"? 有人可以帮帮我,我应该如何引用id为“ draggable”的div元素?
The developer docs at https://developer.mozilla.org/en-US/docs/Web/API/Window.getComputedStyle seem to indicate that the following should succeed: https://developer.mozilla.org/zh-CN/docs/Web/API/Window.getComputedStyle上的开发人员文档似乎表明以下内容应该成功:
var elem1 = document.getElementById("elemId");
var style = window.getComputedStyle(elem1, null);
So, try adding window.
因此,尝试添加window.
to your getComputedStyle
clause, and check that your div is named correctly. 到您的getComputedStyle
子句,并检查您的div是否正确命名。
If neither of those two things help, check your javascript console for errors. 如果这两件事都没有帮助,请检查您的JavaScript控制台是否有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.