質問

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