2012-09-19 5 views
0

HTML 테이블에 http://jsfiddle.net/Lijo/Hb28u/4/에 주어진 jquery 스크립트가 있습니다. 테이블 행을 강조 표시하기위한 4 가지 jQuery 접근 방식이 있습니다. 마지막 두 가지 접근 방식이 작동하지 않습니다. 왜 작동하지 않습니까? 나는 평범한 영어로 설명을 찾고 있습니다. HTML테이블 행 강조 표시가 작동하지 않습니다.

<table id="table1"> 
<tr> <td>N</td><td>Y</td></tr> 
<tr class="y_n"><td>Q</td><td>N</td></tr> 
</table> 

<br/><br/> 

<table id="table2"> 
<tr> <td>N</td><td>Y</td></tr> 
<tr class="y_n"><td>Q</td><td>N</td></tr> 
</table> 

<br/><br/> 

<table id="table3"> 
<tr> <td>N</td><td>Y</td></tr> 
<tr class="y_n"><td>Q</td><td>N</td></tr> 
</table> 

<br/><br/> 

<table id="table4"> 
<tr> <td>N</td><td>Y</td></tr> 
<tr class="y_n"><td>Q</td><td>N</td></tr> 
</table> 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"> 

SCRIPT

$(document).ready(function() 
{ 


//Apporach 1 - Highlight First Row 

$('#table1 tr td:eq(0)').each(function() { 
if ($(this).text() == 'N') { 
    $(this).parent().css('background-color', 'Orange'); 
} 
}); 


//Apporach 2 - - Highlight Second Row 

$('#table2 tr td:gt(0)').each(function() { 
if ($(this).text() == 'N') { 
    $(this).parent().css('background-color', 'Orange'); 
} 
}); 


//Apporach 3 - Highlight Second Row 

$('#table3 tr td:eq(1)').each(function() { 
if ($(this).text() == 'N') { 
    $(this).parent().css('background-color', 'Orange'); 
} 
}); 


//Apporach 4 Highlight All Rows 

$('#table4 tr td)').each(function() { 
if ($(this).text() == 'N') { 
    $(this).parent().css('background-color', 'Orange'); 
} 
}); 

}); 
+0

시도'backgroundColor' ... – Night2

+0

접근 4 : '#의 표 4의 그럴 TD)는' '# 표 4의 그럴 TD' – Prasanna

답변

2

지금은 작동을 가지고 jsfiddle

,
$('#table3 tr td:eq(3)').each(function() { // note that you search for td no. 3 and not 1 
    if ($(this).text() == 'N') { 
     $(this).parent().css('background-color', 'Orange'); 
    } 
}); 


//Apporach 4 Highlight All Rows 
$('#table4 tr td').each(function() { // note that in your example you have a) at the end of the selector 
    if ($(this).text() == 'N') { 
     $(this).parent().css('background-color', 'Orange'); 
    } 
}); 
+0

즉, td에 연속 된 번호가 지정되어 있습니까? – Lijo

+1

선택기'# table3 tr td'는 선택된 tr 안에 모든 tds가있는 배열을 반환하므로 각 td는 찾은 순서대로 자체 색인을 갖습니다. –

+0

설명해 주셔서 감사합니다. 나는 그것을 성능 문제로 생각한다. RowDataBound 이벤트 ASP.Net GridView에서 필요한 행에 대한 CSS 클래스 추가에 갈 것입니다. 그리고 css 클래스를 기반으로 행을 선택하십시오. $ (# myTable tr.myclass ') http://stackoverflow.com/questions/2390200/how-do-i-specify-css-classes-for-specific-rows-in- a-gridview – Lijo

0
//Apporach 3 - Highlight Second Row 
$('#table3 tr:eq(1) td').each(function() { //second row 
if ($(this).text() == 'N') { 
    $(this).parent().css('background-color', 'Orange'); 
} 
}); 


    //Apporach 4 Highlight All Rows 
    $('#table4 tr td').each(function() { //here you had a unnescessary ')' 
    if ($(this).text() == 'N') { 
     $(this).css('background-color', 'Orange'); 
    } 
    }); 

희망 당신은 그것을 :)

+0

번호 당신이 언급 한 접근 방식 3은 접근 1. 접근 (3)하지 않는 한 동일 할 것 그것의 임무를 수행하십시오 - http://jsfiddle.net/Lijo/Hb28u/12/ – Lijo

+0

@Lijo가 내 대답을 업데이트했습니다. 지금 확인하십시오 – coolguy

관련 문제