2008-09-04 11 views
20

.container 많은 .components를 포함하고, 자신이이 같은jQuery : 클래스가 아닌 CSS 규칙으로 선택할 수 있습니까?

감안할 코드 (차례로 .components 등 등을 포함 할 수있는) .containers를 포함 할 수 있습니다 .components 수 있습니다 무엇을

$(".container .component").each(function(){ 
    $(".container", this).css('border', '1px solid #f00'); 
}); 

을 CSS에서 너비가 'auto'로 설정된 중첩 된 .containers 만 선택하려면 중괄호 내의 행에 추가해야합니까? 나는 그것이 단순한 것이라고 확신하지만 실제로 jQuery를 많이 사용하지는 않았다.

+0

많은 감사합니다. CSS 선택기 내에 CSS 규칙을 포함하는 것이 이상적 이었으므로 CSS 폭 : auto이지만 .component에있는 모든 .containers 그룹을 가질 수있었습니다. –

답변

18
$(".container .component").each(function() 
{ 
    $(".container", this).each(function() { 
     if($(this).css('width') == 'auto') 
     { 
      $(this).css('border', '1px solid #f00'); 
     } 
    }); 
}); 
다른 대답 유사

하지만 이후 구성 요소는 또한 .each() 폭이 너무 여기에서 확인이 필요, 여러 용기를 가질 수 있습니다.

4
$(".container .component").each(function() { 
    if ($(".container", this).css('width') === "auto") 
     $(".container", this).css('border', '1px solid #f00'); 
}); 
16

.filter()을 살펴볼 수 있습니다. 같은

뭔가 : 귀하의 답변에 대한

$('.container .component .container') 
.filter(function() {return $(this).css('width') == 'auto';}) 
.css({border: '1px solid #f00'}); 
+0

이것은 내가 뭘 찾고, 달성하기 위해 단일 라인입니다. –