[英]Change tr background-color
我有這樣的事情:
<tr id='<%=currentRow %>' onclick="SetBackgroundColor(this)" style="background-color:Yellow">
當我點擊一行我想改變它的背景顏色,我喜歡這樣:
function SetBackgroundColor(rowId)
{
$(rowId).css("background-color", "#000000");
}
但我不知道為什么它不起作用。 有什么建議嗎?
IE有TR元素的背景顏色問題。 更安全的方法是在TR中設置TD和TH的背景:
<table id="tabletest">
<tr>
<td>testcell</td>
</tr>
</table>
<script>
$('#tabletest tr').bind('click', function(e) {
$(e.currentTarget).children('td, th').css('background-color','#000');
})
</script>
補充:您可以為整個表分配單個事件處理程序以提高性能:
$('#tabletest').bind('click', function(e) {
$(e.target).closest('tr').children('td,th').css('background-color','#000');
});
在jQuery中,您不必使用onclick屬性來分配事件處理程序。 假設您為要影響的每個tr添加一個名為mytr的類。 然后你可以做這樣的事情:
$(document).ready(function(){
$(".mytr").click(function(){
$(this).css("background-color", "#000000");
});
});
這將把事件處理程序應用於具有類mytr的所有行。
這將在點擊新行時重置每一行......
$(document).ready(function(){
$('tr').click(function(){
$('tr td').css({ 'background-color' : 'green'});
$('td', this).css({ 'background-color' : 'red' });
});
});
演示: http : //jsbin.com/aciqi/
$('#RowID').children('td, th').css('background-color','yellow');
一個更簡單的解決方案是可能對表或addClass中的所有行使用選擇器。
例
$("#myTable tr").click(function() {
$(this).css('background-color', '#f00');
});
要么
$("#myTable tr").click(function() {
$(this).addClass('selected');
});
請嘗試更改表格單元格背景顏色,而不是更改表格行背景顏色。
$(document).ready(function() {
$(".mytr td").click(function() {
$(this).css("background-color", "#000000");
});
});
謝謝大家......問題是在masterpage中我before
query-1.3.2-vsdoc.js
before
加載了j query-1.3.2.min.js
,這樣它就不能query-1.3.2-vsdoc.js
再次query-1.3.2-vsdoc.js
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.