Jquery 上一个对象选择器

Jquery previous object selector

我对 jquery 上的上一个对象有疑问,它没有给我位置... 我有带有“.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>

我不能 select prev 作为一个对象

至于 jQuery 文档:

.prev( [selector ] ) Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.

Reference

您的 <div class="post-image"> 元素没有 立即 前面的元素具有相同的 class。它前面的元素是 <div class="post">

[编辑]
你可以这样走:

// 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; 

});