<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> JS obtains and displays mouse coordinate values in real time </title> </Head> <Body> <Script type = "text/javascript"> Var getcoordin1_entexample = function (){ Var coords = document. getElementById ("coords "); Coords. onmousemove = function (e ){ Var pointer = getCoordInDocument (e ); Var coord = document. getElementById ("coord "); Coord. innerHTML = "X, Y = (" + pointer. x + "," + pointer. y + ")"; } } Var getCoordInDocument = function (e ){ E = e | window. event; Var x = e. pageX | (e. clientX + (Document.doc umentElement. scrollLeft | Document. body. scrollLeft )); Var y = e. pageY | (e. clientY + (Document.doc umentElement. scrollTop | Document. body. scrollTop )); Return {'X': x, 'y': y }; } Window. onload = function (){ Getcoordin1_entexample (); }; </Script> <Div id = "coords" Style = "width: 500px; height: 200px; background: # F2F1D7; Border: 2px solid # 0066cc; "> Move the mouse here. </Div> <Br/> <Div id = "coord" Style = "width: 500px; border: 2px solid #336699;"> </div> </Body> </Html> |