日期:2014-05-16  浏览次数:20283 次

求解:坐标问题
有两个按钮id值为b1,b2。我想将b1.id和b2.id两值作为参数传给JS函数。在JS函数中找到b1   b2的xy坐标值。该js函数该怎么写?

------解决方案--------------------
这是一个例子
--------------------------
<!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> 无标题文档 </title>
<script language= "javascript ">
function Point(iX, iY){
this.x = iX;
this.y = iY;
}

function fGetXY(aTag){ //传入目标对象
var oTmp = aTag;
var pt = new Point(0,0);
do {
pt.x += oTmp.offsetLeft;
pt.y += oTmp.offsetTop;
oTmp = oTmp.offsetParent;
} while(oTmp.tagName!= "BODY ");
return pt;
}

function showPosition(obj){
var pt=fGetXY(obj);
alert( 'X坐标为: '+pt.x+ '\nY坐标为: '+pt.y);
}
</script>
</head>

<body>
<table width= "100% " border= "0 " cellspacing= "50 " cellpadding= "1 ">
<tr>
<td width= "33% "> <input name= "button " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td width= "34% "> <input name= "button2 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td width= "33% "> <input name= "button3 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
</tr>
<tr>
<td> <input name= "button7 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td> <input name= "button6 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td> <input name= "button4 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
</tr>
<tr>
<td> <input name= "button8 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td> <input name= "button62 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
<td> <input name= "button5 " type= "button " value= "看看我的坐标 " onclick= "showPosition(this) "/> </td>
</tr>
</table>
</body>
</html>