开发者

jQuery selector for the child of a parent's previous sibling

开发者 https://www.devze.com 2023-02-15 14:31 出处:网络
For each stat block like the following: <div class=\"stats\"> <div class=\"label\"> <span class=\"goal\">10 YEARS OF SOMETHING GOOD</span>

For each stat block like the following:

<div class="stats">
  <div class="label">
    <span class="goal">10 YEARS OF SOMETHING GOOD</span>
  </div>
  <div class="stat">
    <div class="tri"></div>
  </div>
</div>

I want to set the marginRight of tri to half the width of the previous goal. Which jQuery 开发者_运维知识库selector would I use to grab the previous 'goal' relative to each tri ?

Something like..

$(function(){
  $('.tri').css({marginRight: function() {
    return Math.floor($(this).something('.goal').width() / 2) + 'px';
  }})
})


Use:

$(this).parent().prev().find('.goal').width()

This also works:

$(this).parents('.stats').find('.label > .goal').width()
0

精彩评论

暂无评论...
验证码 换一张
取 消