Javascript 如何更新坐标而不是使它们重叠?

Javascript 如何更新坐标而不是使它们重叠?,javascript,Javascript,我有一个程序,可以在给定的画布上写下鼠标所在位置的坐标。我遇到的问题是,它不会更新它们,但每次移动鼠标时都会重叠。经过长时间的思考,我不知道如何解决这个问题 /* This program displays the x and y * coordinates in a label on the screen * and updates when the mouse moves */ var text; function start(){ mouseMoveMethod(coord

我有一个程序,可以在给定的画布上写下鼠标所在位置的坐标。我遇到的问题是,它不会更新它们,但每次移动鼠标时都会重叠。经过长时间的思考,我不知道如何解决这个问题

/* This program displays the x and y
 * coordinates in a label on the screen
 * and updates when the mouse moves */

var text;

function start(){
    mouseMoveMethod(coords);
} 

function coords(e){
    var text = new Text(e.getX() + ", " + e.getY() , "30pt Arial");
    text.setPosition(0,30);
    add(text);
    text.setText(e.getX() + ", " + e.getY());
}

每次调用coords时,它都会创建一个新文本并覆盖到所有其他文本上。
您希望重用某些内容并对其进行更新

试试这个(适当充实):


功能坐标(e)
{c=document.getElementById(“coordsdiv”);
c、 innerHTML=“(“+e.getX()+”,“+e.getY()+”)”;
}

如果您发布所有代码,这将非常好,如果您将其发布在代码片段上,则更好。干杯如果我没说错的话,你现在面临的问题是拖拽画布,不是吗?
<div id=coordsdiv style="position:absolute;left:0;top: 30"> </div>

<script>  
function coords(e)
{c = document.getElementById("coordsdiv");
 c.innerHTML = "(" + e.getX() + ", " + e.getY() + ")";
}
</script>