jquery - 将 margin-left 添加到 div

标签 jquery css

我这里有很多 div,我想将 55px 添加到所有 margin-left,而不是将它设置为 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? ;

最佳答案

遍历它们并将 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>

关于jquery - 将 margin-left 添加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035471/

相关文章:

javascript - 在 symfony2 中使用 viewer.js 插件

CSS:将边框颜色设置为 parent 的背景颜色

javascript - javascript中的动态 Accordion

javascript - 让灯箱看起来像另一个盒子

javascript - 实际上不向服务器提交任何内容的 HTML 表单(JavaScript 中的客户端处理)

javascript - jQuery AutoNumeric 不格式化多个字段

Javascript setInterval 每 5 秒

javascript - 回调函数——javascript

javascript - 为移动设备实现切换菜单,但整个导航栏都是可点击的,我只想点击按钮

javascript - 在具有指定坐标的 div 内绘制一条线