문제

I usually use jQuery but am starting to develop in pure javascript. I'm trying to create a variable for an element that I can use throughout the code but I can't seem to get it working. At the moment I'm trying:

var canvas = document.getElementById('canvas').value;

Then using that variable like:

canvas.style.backgroundPosition = '0 0';

Can someone please tell me what I'm doing wrong that is cause the variable to not be read or not declared properly?

도움이 되었습니까?

해결책

element.value returns the value as a string, not the element (and a canvas has no value).
For just the element remove the .value part

var canvas = document.getElementById('canvas');

canvas.style.backgroundPosition = '0 0';
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top