$("#justfortest").click(function(){
alert( $("#mydata_records").scrollTop()); $("#mydata_records").find(".mydata_records_record").each(function(i){
alert($(this).position().top);
alert($(this).html());
});
});
为什么我alert($(this).position().top)的值都是一样的呀?????????
但是alert($(this).html())的值却是不同的。
不明白了,我用的是chrome 应该没有什么关系呀???而且 我用.offset().top值都是一样的
谢谢大家。
alert( $("#mydata_records").scrollTop()); $("#mydata_records").find(".mydata_records_record").each(function(i){
alert($(this).position().top);
alert($(this).html());
});
});
为什么我alert($(this).position().top)的值都是一样的呀?????????
但是alert($(this).html())的值却是不同的。
不明白了,我用的是chrome 应该没有什么关系呀???而且 我用.offset().top值都是一样的
谢谢大家。
var offsetParent = this.offsetParent(),
// Get correct offsets
offset = this.offset(),
parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
// Subtract element margins
// note: when an element has margin: auto the offsetLeft and marginLeft
// are the same in Safari causing offset.left to incorrectly be 0
offset.top -= num( this, ’marginTop’ );
offset.left -= num( this, ’marginLeft’ );
// Add offsetParent borders
parentOffset.top += num( offsetParent, ’borderTopWidth’ );
parentOffset.left += num( offsetParent, ’borderLeftWidth’ );
// Subtract the two offsets
results = {
top: offset.top - parentOffset.top,
left: offset.left - parentOffset.left
};使用position()方法时事实上是把该元素当绝对定位来处理,获取的是该元素相当于最近的一个拥有绝对或者相对定位的父元素的偏移位置。使用position()方法时如果其所有的父元素都为默认定位(static)方式,则其处理方式和offset()一样,是当前窗口的相对偏移使用offset()方法不管该元素如何定位,也不管其父元素如何定位,都是获取的该元素相对于当前视口的偏移知道了这些特点,我们应该如何来合理的使用position()和offset()呢?就我个人的经验,通常获取一个元素(A)的位置是为了让另外的一个元素(B)正好出现在A元素的附近。通常有2种情况:1.要显示的元素B存放在DOM的最顶端或者最底端(即其父元素就是body).这个时候用offset()是最好的