獲取頁面某一元素的絕對X,Y坐標,可以用offset()方法:(body屬性設置margin :0;padding:0;) var X = $('#DivID').offset().top; var Y = $('#DivID').offset().left; 獲取相對(父元素)位置: var X = $('#DivID').position().top; var Y = $('#DivID').position().left;
<!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=GBK" /><title>java Test - Liehuo.net</title></head><style type="text/css"><!--body,div { margin:0; padding:0;}--></style><script type="text/javascript" src="http://www.vxbq.cn/uploads/common/jquery-1.3.2.min.js"></script><body><div style="background:#ccc;height:300px;" onclick=""></div><div style="position:relative;"><div style=" position:absolute;left:50px; top:50px;" id="DivID"></div></div><script type="text/javascript">var X = $('#DivID').offset().top;var Y = $('#DivID').offset().left;document.write(X+"<br />");document.write(Y+"<br />");//獲取相對(父元素)位置:var C = $('#DivID').position().top;var D = $('#DivID').position().left;document.write(C+"<br />");document.write(D);</script></body></html><br /><center>如不能顯示效果,請按Ctrl+F5刷新本頁,更多網頁代碼:<a href='http://www.vxbq.cn/' target='_blank'>http://www.vxbq.cn/</a></center>提示:可修改后代碼再運行!
上一篇 如何有效提高操作系統和IIS的安全可靠性
下一篇 .NET調PHP Web Service的典型例子
程序員人生,我編程,我富裕,記住wfuyu網,php教程,php學習,php手冊,CMS模版制作
聲明:本站大部分內容是作者原創,少部分收集于互聯網供大家一起學習,原版權很多不明,如有侵權請聯系本站,謝謝!
粵ICP備14040726號-1?? 2015-2020 程序員人生 版權所有