我有两个文本字段imageWidth和imageHeight ...我想从它们中获取输入,添加当前鼠标的poointer位置,然后做一些事情:
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
var wWidth = document.getElementById('imageWidth').value;
var wHeight = document.getElementById('imageHeight').value;
var b = x + wWidth;
alert(b);
if(x+wWidth < 800 && y+wHeight < 1560)
{
//some work here
}
因此,如果用户输入宽度为400且x返回151 ... b&#39; s的值将为151400 ...而我希望它为551以使循环正常工作...
答案 0 :(得分:2)
您应该阅读parseInt()
方法 - &gt; parseInt() on MDN
答案 1 :(得分:2)
这应该有效:
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
var wWidth = document.getElementById('imageWidth').value;
var wHeight = document.getElementById('imageHeight').value;
var x1 = parseInt(x, 10) + parseInt(wWidth, 10);
var y1 = parseInt(y, 10) + parseInt(wHeight, 10);
if(x1 < 800 && y1 < 1560)
{
//some work here
}
答案 2 :(得分:0)
Javascript是一种隐式输入的语言。这意味着你必须告诉它你想做数学时的事物类型。你的代码只是告诉它连接字符串,因此你看到的结果。
试试这个:
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
var wWidth = document.getElementById('imageWidth').value;
var wHeight = document.getElementById('imageHeight').value;
var b = parseInt(x, 10) + parseInt(wWidth, 10);
alert(b);
if(x+wWidth < 800 && y+wHeight < 1560)
{
//some work here
}