我有一个关于jquery的prev对象的问题它没有给我位置... 我的div有“.post-image”
$(".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作为对象
答案 0 :(得分:0)
至于jQuery文档:
.prev( [selector ] )
获取匹配元素集中每个元素的立即前兄弟,可选择由选择器进行过滤。
您的<div class="post-image">
元素在同一个类的前面元素中没有立即。它前面的元素是<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;
});