jQuery:클래스가 아닌 CSS 규칙으로 선택할 수 있나요?
-
09-06-2019 - |
문제
.container에는 많은 .comComponents가 포함될 수 있으며 .comComponents 자체에는 .containers가 포함될 수 있습니다(이는 차례로 .comComponents 등을 포함할 수 있음).등.)
다음과 같은 코드가 주어졌습니다.
$(".container .component").each(function(){
$(".container", this).css('border', '1px solid #f00');
});
CSS에서 너비가 'auto'로 설정된 중첩된 .container만 선택하려면 중괄호 안의 줄에 무엇을 추가해야 합니까?나는 그것이 간단한 것이라고 확신하지만 실제로 jQuery를 그렇게 많이 사용하지는 않았습니다.
해결책
$(".container .component").each(function()
{
$(".container", this).each(function() {
if($(this).css('width') == 'auto')
{
$(this).css('border', '1px solid #f00');
}
});
});
다른 답변과 유사하지만 구성 요소에는 여러 컨테이너가 있을 수 있으므로 너비에 대해서도 여기에서 .each() 체크인이 필요합니다.
다른 팁
당신은 조사하고 싶을 수도 있습니다 .filter()
.
다음과 같은 것 :
$('.container .component .container')
.filter(function() {return $(this).css('width') == 'auto';})
.css({border: '1px solid #f00'});
$(".container .component").each(function() {
if ($(".container", this).css('width') === "auto")
$(".container", this).css('border', '1px solid #f00');
});
제휴하지 않습니다 StackOverflow