2011-05-13 66 views

回答

1

可悲的是,有沒有辦法風格CSS父元素,所以你必須使用JavaScript。

-1

這個jQuery代碼應該這樣做:

$('input').focus(function() { 
    $('tr').css("background-color", "#c00"); 
}); 

演示here

+0

請參閱:http://jsfiddle.net/9mR95/當代碼被選中時,該代碼將突出顯示所有行,而不僅僅是輸入元素的父代,並且不會在模糊後忽略該行。 – Ryan 2011-05-16 18:05:28

1

這是一件好事,當你正在生成的許多相似的行(循環通過大型數據集等): 腳本:

function rowSet(data_id){ 
    document.getElementById('row_' + data_id).style.backgroundColor = 'blue'; 
} 
function rowReset(data_id){ 
    document.getElementById('row_' + data_id).style.backgroundColor = ''; 
} 

身體:

<body> 
    <form> 
     <table> 
      <tr id="row_#data_id#"> 
       <td><input name="input1_#data_id#" onFocus="rowSet(#data_id#);" onBlur="rowReset(#data_id#);"></td> 
       <td><input name="input2_#data_id#" onFocus="rowSet(#data_id#);" onBlur="rowReset(#data_id#);"></td> 
      </tr> 
     </table> 
    </form> 
</body> 

你也可以使用currentRow,或任何你喜歡的。

5

使用JQuery,這是非常可能的。注意:

HTML

<table border="1" cellpadding="20"> 
    <tr> 
     <td>Text</td> 
     <td height="50" width="100" id="somename"><input type="text" value="" id="mirza"></td> 
    </tr> 
    <tr><td>&nbsp;</td><td>&nbsp;</td></tr> 
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr> 
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr> 
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr> 
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr> 
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr> 
</table> 

CSS

.highlightedRow { background-color: orange; } 

jQuery的

$('input').focus(function() { 
    $(this).parent().parent().addClass('highlightedRow'); 
}); 

$('input').blur(function() { 
    $(this).parent().parent().removeClass('highlightedRow'); 
}); 
+0

+1因爲它和替代方案,併爲我工作。 – 2012-10-03 16:47:20