2011-09-08 3 views
1

요소를 반복하고 싶습니다. 각 라인 요소는 하위 요소로 항목입니다. 먼저 을 반복 한 다음 특정 항목 요소를 지정하고 싶습니다. 세 번째.jQuery : 특수 클래스를 사용하여 하위 항목을 처리하는 방법

$(".pricetable .righttable .line:not(.blank)").each(function(j) { 
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j); 
    currentLine.(".item").each(function(k) { 
     $(".pricetable .righttable .line .item").eq(i).addClass("active"); 
    }); 
}); 

문제는 내가 항목 클래스와 자식 요소를 해결하는 방법을 모르는 것입니다 : 이것은 내가 가지고있는 코드입니다.

currentLine.(".item").each(function(k) 

이것은 작동하지 않습니다.

XML 필터는 비 XML 값 ({0 : # 2 = ({}), 길이 : 1, prevObject : 길이 : 3, prevObject : {0 : # 1 = ({}) : 0 : # 2 #, 1 : ({}), 2 : ({1}, 길이 : 1}, 컨텍스트 : # 1 #, 선택자 : "pricetable .righttable .line : not (.blank) (0,1) "}) file : /// C :/Users/xxx/lib/lib : pricetable.js 라인 (112)

편집 :
와우, 나는 그런 좋은 빠른 응답을 생각하지 않았다! 나는이 솔루션과 함께 갈 생각 : 그들은 currentLine 직접 아이들을 경우

$(".pricetable .righttable .line:not(.blank)").each(function(j) { 
    $(this).children(".item").eq(i).addClass("active"); 
}); 

답변

3
$(".pricetable .righttable .line:not(.blank)").each(function(j) { 
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j); 
    currentLine.find(".item").each(function(k) { 
     $(".pricetable .righttable .line .item").eq(i).addClass("active"); 
    }); 
}); 

http://api.jquery.com/find/

, 당신은 단지 find('.item') 대신에 children('.item')을 할 수 있어야합니다. 이를 DOM 탐색 (Traversing the DOM)이라고합니다. 이 페이지 (http://api.jquery.com/category/traversing/)를 방문하여 몇 가지 기능에 대한 설명을 읽어보십시오. 당신이 많은 :

line의 n 번째 item 뭔가를하고 싶은 경우
2
$('.pricetable .righttable .line:not(.blank)').find('.item').each(function() { 
    // this point to child element - item 
    // use $(this).addClass('active'); 
}); 
2

, 그것은 보이는 것보다 훨씬 쉽게를 통과하면 그들은 명심하는 것이 매우 유용 할 것입니다 :

var index = 3; // the third "item" element 
$(".pricetable .righttable .line:not(.blank) .item:nth-child(" + index + ")") 
    .addClass("active"); 

See it in action.

위의 내용이 충분하지 않다는 내용이 있습니까?

2
나는 당신의 질문에 대답

약간 개선 전 :

var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j); 

가 쉽게 교체 할 수 있습니다 : 당신이 $(this)의 내부에 3 .item 요소를 발견하고자하는 경우,

var currentLine = $(this); 

둘째, 사용 .find()

.find(".item:nth-child(3)"); 

회선을 반복 할 필요가 없습니다.줄을 반복해야한다면 k2 (JavaScript가 0부터 계산)과 비교하면됩니다.

1
$(".pricetable .righttable .line:not(.blank)").each(function() { 
    $(this).find('.item:nth-child(2)').addClass('active'); 
}); 
관련 문제