JQuery中得到Element真实top、left、height和width属性值的对象

在一个电子商务网站的实施过程中,发现使用的JQuery1.3.2获得Element的实际的top、left、height和width时,对于IE浏览器和Chrome需要使用不同的代码来得到。为了便于统一使用,我 自己写了一个对象,在这里记录下来,以备将来参考。
JQuery的版本为1.3.2
该对象的代码如下:
var isIE = $.browser.msie;
function JQElement(){
this.obj;
this.top;
this.left;
this.height;
this.width;

this.getE = function(objid) {
if(isIE){
this.obj = document.getElementById(objid);
this.top = this.obj.offsetTop;
this.left = this.obj.offsetLeft;
this.height = this.obj.offsetHeight;
this.width = this.obj.offsetWidth;
}else{
this.obj = $("#" + objid);
this.top = this.obj.offset().top;
this.left = this.obj.offset().left;
this.height = this.obj.height();
this.wight = this.obj.width();
}

return this;
}
}


调用该对象的代码为:
var $searchdiv = new JQElement().getE("search_div");
var searchtop = $searchdiv.top;
var searchleft = $searchdiv.left;
var searchwidth = $searchdiv.width;
var searchheight = $searchdiv.height;
delete $searchdiv;


通过该对象的编写,在使用JQuery获得div元素的实际位置时,代码量减少了不少。同时对于JQuery在其他的浏览器出现可能的问题时,修改起来也会更加方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值