2015-01-19 5 views
0

여기에 많은 div가 있고 55px를 모든 여백에 추가하려고합니다. 그러나 55px로 설정하지 않고 이미 여기에 몇 가지 코드가 추가되어 있습니다.div에 왼쪽 여백 추가하기

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

답변

3
그들을 통해

루프를 현재 margin-left55을 추가합니다.

$("#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'; 
    }) 
}); 

작업 예. `$ (이) .CSS ('여백 왼쪽', 기능 (전, 발) {반환 발 + :

$('#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>

+1

당신은 두 번 CSS()''호출 할 필요는 없습니다 55;})'는 마찬가지로 작동해야하며, val은 margin-left 속성의 현재 값입니다. –

+0

그것을 사용할 때 내 콘솔에서이 오류가 발생합니다. 잡히지 않은 SyntaxError : 예기치 않은 토큰, – Nick

+0

그만하십시오 !!! 누구든지 연재 upvoting입니다. :( –