2011-05-13 71 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