2012-01-11 5 views
0

jquery에 대한 멍청한 반응입니다. 붙어서 도움을 청할 것이다. 현재 특정 페이지에서 데이터를 가져 오는 도구를 작성 중입니다. 페이지는 다음과 같습니다 : 나는 아이디 parent0 및 child0와 단지 <tr> 블록에서 모든 데이터를 수집하기 위해 노력하고있어jquery는 id를 기반으로 한 요소를 일치시킵니다.

ancestor = $(this).closest("tr[id]"); 

    matchedElement = $(this).first(); 
    originalBgColor = $(matchedElement).css('background-color'); 
    $(matchedElement).css('background-color', 'green'); 
    $(matchedElement).bind('click.annotator', function(event) { 
    event.stopPropagation(); 
    event.preventDefault(); 
    self.port.emit('show', 
     [ 
     document.location.toString(), 
     $(ancestor).attr("child0"), 
     $(matchedElement).text() 
     ] 

:

다음
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1"> 
<tbody> //This is data group 1  
    <tr id="parent0"> //Record 1 
    <td align="left"> <---------- text1 here -------> </td> 
    <td align="left"> <---------- text2 here -------> </td> 
    <td align="left"> <---------- text3 here -------> </td> 
    </tr>  
    <tr id="parent0"> //Record 2 
    <td align="left"> <---------- text1 here -------> </td> 
    <td align="left"> <---------- text2 here -------> </td> 
    <td align="left"> <---------- text3 here -------> </td> 
    </tr>  
</tbody> 
</table> 

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1"> 
<tbody> //This is data group 2  
    <tr id="child0"> //Record 1 
    <td align="left"> <---------- text1 here -------> </td> 
    <td align="left"> <---------- text2 here -------> </td> 
    <td align="left"> <---------- text3 here -------> </td> 
    </tr>  
    <tr id="child0"> //Record 2 
    <td align="left"> <---------- text1 here -------> </td> 
    <td align="left"> <---------- text2 here -------> </td> 
    <td align="left"> <---------- text3 here -------> </td> 
    </tr>  
</tbody> 
</table> 

가 JQuery와의 조각입니다.

현재 도구는 두 테이블의 모든 데이터를 텍스트로 캡처합니다. 이상적으로는 모든 <TR> 블록을 개별적으로 캡처 할 수 있도록 배열에 넣을 수있게하고 싶습니다.

+3

의 ID 값은 고유해야합니다, 당신은 현재 여러 요소에 동일한 ID를 할당하고 있습니다 : 사실,이 솔루션은 더 우아한라고 생각합니다. – Paul

+2

ID를 두 번 이상 사용하지 마십시오. http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 –

답변

0
$('tr[id="child0"]').each(function() { 
         //this will be fired for each matched element 
         $(this).doSomething(); 
         } 
        ); 

처럼 당신이 ID child0 검사가 http://api.jquery.com/category/selectors/

+0

감사합니다. 그것은 내 문제를 파악하는 데 도움이되었습니다. – spex2004

2

클래스를 사용해야하는 곳에서는 ID 요소를 사용하고있는 것처럼 보입니다. 대신

<tr id="child0"> 
<tr id="parent0"> 

는이

<tr class="child0"> 
<tr class="parent0"> 

ID 달리,이 클래스의 속성을 가진 여러 요소에 같은 값을 할당 할 수 있습니다 마십시오.

그럼 당신은이

$("tr.child0,tr.parent0").each(
          function() { 
          //this will be fired for each matched element 
          $(this).doSomething(); 
          } 
         ) 
0

그렇게하십니까 기준이 링크 모든 TR을 얻을 것이다 이런 식으로 그들 모두를 선택할 수 있습니다 ..

$('cite.fn:contains(blabla)').css('color', 'red'); 

편집 : "blablablabla"와 일치합니다. 엘.

$('cite.fn').each(function() { 
    if ($(this).text() == 'blabla') { 
     $(this).css('color', 'red'); 
    } 
}); 

더 정확해야합니다.

편집 :

$('cite.fn').filter(function() { 
    return $(this).text() == 'blabla'; 
}).css('color', 'red');; 
관련 문제