向div添加margin-left

时间:2015-01-19 23:41:47

标签: jquery css

我这里有很多div,我想将55px添加到所有边距 - 左边没有设置为55px但是将它添加到已经存在的内容这里有一些代码。

$("#AfirstObj,#AsecondObj,#AthirdObj,#AfourthObj, #AfithObj,#AsixthObj,#AseventhObj,#AeightObj,#AnineObj,#AtenthObj,#AeleventhObj,#AtwelveObj,#AthirteenObj,#AfourteenObj,#AfifthteenObj,#AsixteenObj, #AseventeenObj,#AeighteenObj,#AnineteenObj").what do i add here? ;

1 个答案:

答案 0 :(得分:3)

循环浏览它们并将55添加到当前margin-left值。

$("#AfirstObj,#AsecondObj,#AthirdObj,#AfourthObj, #AfithObj,#AsixthObj,#AseventhObj,#AeightObj,#AnineObj,#AtenthObj,#AeleventhObj,#AtwelveObj,#AthirteenObj,#AfourteenObj,#AfifthteenObj,#AsixteenObj, #AseventeenObj,#AeighteenObj,#AnineteenObj").each(function() {
  $(this).css('margin-left', function(i, val) {
    return String(parseInt(val) + 55) + 'px';
  })
});

工作示例。

$('#one, #two, #three, #four').each(function() {
  $(this).css('margin-left', function(i, val) {
    return String(parseInt(val) + 55) + 'px';
  })
})
#one, #two, #three, #four {
  display: inline-block;
  width: 100px;
  height: 100px;
  background: tan;
}
#one, #three {
  margin-left: 5px;
}
#two, #four {
  margin-left: 55px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>