我对jquery上的prev对象有问题,它没有给我位置。。。我有带有".post image"的div
$(".post-image").each(function(){
var div = $(this);
var index = div.index();
var odd = index % 2;
if(odd == 1){
var sibling = div.next();
sibling.css({
margin:"15px",
marginTop:"-165px"
})
var bElement= div.prev(".post-image");
console.log(bElement)
})
HTML:
<div class="post">
<div class="post-header"></div>
<div class="post-content"></div>
</div>
<div class="post-image">
<div class="post-header"></div>
<div class="post-content"></div>
</div>
我无法选择prev作为对象
对于jQuery文档:
.prev( [selector ] )
立即获取匹配元素集中每个元素的前一个同级元素,可选地由选择器进行筛选。
参考
您的<div class="post-image">
元素没有立即在前的具有相同类的元素。它前面的元素是<div class="post">
[EDIT]
你可以走这条路:
// declare bElement variable:
var bElement;
$(".post-image").each(function(){
var div = $(this);
var index = div.index();
var odd = index % 2;
if(odd == 1){
var sibling = div.next();
sibling.css({
margin:"15px",
marginTop:"-165px"
});
// make sure that previous element with class '.post-image' exists :
if(bElement !== undefined){
var prevElemPosition = bElement.position().top;
// do something ...
}
}
// set latest .post-image element (so that it will be the previous on next iteration):
bElement = div;
});