在一个电子商务网站的实施过程中,发现使用的JQuery1.3.2获得Element的实际的top、left、height和width时,对于IE浏览器和Chrome需要使用不同的代码来得到。为了便于统一使用,我 自己写了一个对象,在这里记录下来,以备将来参考。
JQuery的版本为1.3.2
该对象的代码如下:
调用该对象的代码为:
通过该对象的编写,在使用JQuery获得div元素的实际位置时,代码量减少了不少。同时对于JQuery在其他的浏览器出现可能的问题时,修改起来也会更加方便。
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在其他的浏览器出现可能的问题时,修改起来也会更加方便。