<! 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> draw a line between two points </title> <Style type = "text/css"> Body { Font-size: 12px; } </Style> </Head> <Script type = "text/javascript"> <! -- Var dot = new Array (); Document. onmousedown = function () { // Delete a div if it exists If (document. getElementById ('line _ div ')){ Var clearDiv = document. getElementById ("line_div "); ClearDiv. parentNode. removeChild (clearDiv ); } // Create an event when you press it. If (! A) a = window. event; // Obtain the X and Y axes Var x = a. clientX; Var y = a. clientY; // When the array length is greater than or equal to 4, clear the Array If (dot. length> = 4) dot. splice (0, 4 ); // Add x and y to the array. The array stores two sets of coordinate values, which are equivalent to two points on the page: A (x1, y1) and B (x2, y2 ). Dot. push (x, y ); // Draw a line when the array length is 4. If (dot. length = 4 ){ // Calculate the length and width of the div Var width = Math. abs (dot [0]-dot [2]); Var height = Math. abs (dot [1]-dot [3]); // Locate the specific position in the upper left corner of the div on the page Var left = dot [0]-dot [2] <0? Dot [0]: dot [2]; Var top = dot [1]-dot [3] <0? Dot [1]: dot [3]; // Create a div Var div = document. createElement ("div "); Div. innerHTML = '<div id = "line_div" style = "width:' + width + 'px; height: '+ height + 'px; position: absolute; visibility: visible; left: '+ left + 'px; top:' + top + 'px; border-left: 1px solid # cdcdcd; border-top: 1px solid # cdcdcd; "> </div> '; Document. body. appendChild (div ); } } --> </Script> <Body> </Body> </Html> |