变量未定义

时间:2017-06-17 00:07:03

标签: javascript variables

因此,我尝试在以下代码中获取变量ymyElement,以确定具有类undefined的图像的位置。单击它时,应将顶部和左侧样式设置为变量所在的位置(光标位置)。当我测试它时,它默认为<!doctype html> <html> <body onclick="getCoord(event)" style="height:2000px;"> <h2>click</h2> <p id="display"></p> <script> function getCoord(event) { var x = event.clientX; var y = event.clientY; var coord = "x:" + x + "y:" + y; document.getElementById("display").innerHTML = coord; console.log(x); console.log(y); document.getElementById('myElement').style.position = "absolute"; document.getElementById('myElement').style.top = y; //or whatever document.getElementById('myElement').style.left = x; // or whatever] }; </script> <img id="myElement" src="https://picturethismaths.files.wordpress.com/2016/03/fig6bigforblog.png?w=419&h=364"> </body> </html>

ind = find(~Population); % find zero places
Population(ind) = randi(4,1,length(ind)); % replace them with a random integer

1 个答案:

答案 0 :(得分:0)

CSS测量

您忘记添加the measurement数字将被解释为。

&#13;
&#13;
<!DOCTYPE html>
<html>
<body onclick="getCoord(event)" style="height:2000px;">
<h2>click</h2>
<p id="display"></p>
<script>
function getCoord(event) {
	var x = event.clientX;
	var y = event.clientY;
	var coord = "x:" + x + " y:" + y;
	document.getElementById("display").innerHTML = coord;
	console.log(x);
	console.log(y);
  var myEle = document.getElementById('myElement'); // no need to use 3 times
	myEle.style.position = "absolute";
	myEle.style.top = y + "px"; // plus "px"
	myEle.style.left = x + "px"; // plus "px"
};
</script>
<img id="myElement" src="https://picturethismaths.files.wordpress.com/2016/03/fig6bigforblog.png?w=419&h=364">
</body>
</html>
&#13;
&#13;
&#13;