我这里有很多 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/