簡體   English   中英

使用 jQuery 更改單擊表格行的顏色

[英]Changing the color of a clicked table row using jQuery

我需要你的幫助,

我怎樣才能使用 jQuery,

更改表格中所選行的背景顏色(對於本例,我們使用 css class“突出顯示”

如果再次單擊同一行,將其更改回默認顏色(白色) select css class "nonhighlighted"

<!DOCTYPE html>

<html>

<head>

<style type="text/css">

.highlighted {
    background: red;
}
.nonhighlighted {
    background: white;
}
</style>

</head>

<body>

<table id="data" border="1" cellspacing="1" width="500" id="table1">
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>

</body>

</html>
.highlight { background-color: red; }

如果你想多選

$("#data tr").click(function() {
    $(this).toggleClass("highlight");
});

如果您只想一次選擇表格中的 1 行

$("#data tr").click(function() {
    var selected = $(this).hasClass("highlight");
    $("#data tr").removeClass("highlight");
    if(!selected)
            $(this).addClass("highlight");
});

另請注意,您的 TABLE 標簽有 2 個 ID 屬性,您不能這樣做。

創建一個應用行顏色的 css 類,並使用 jQuery 打開/關閉該類:

CSS:

.selected {
    background-color: blue;
}

jQuery:

$('#data tr').on('click', function() {
    $(this).toggleClass('selected');
});

第一次單擊將添加類(使背景顏色為藍色),下一次單擊將刪除該類,將其恢復為之前的狀態。 重復!

就您已經擁有的兩個 CSS 類而言,我將更改.nonhighlighted類以默認應用於表格的所有行,然后打開和關閉.highlighted

<style type="text/css">

.highlighted {
    background: red;
}

#data tr {
    background: white;
}

</style>

$('#data tr').on('click', function() {
    $(this).toggleClass('highlighted');
});

這是一個可能的解決方案,可以為表格的整行着色。

CSS

tr.highlighted td {
    background: red;
}

jQuery

$('#data tr').click(function(e) {
    $('#data tr').removeClass('highlighted');
    $(this).toggleClass('highlighted');   
});

演示: http : //jsfiddle.net/jrthib/HVw7E/2/

在你的 css 中:

.selected{
    background: #F00;
}

在jQuery中:

$("#data tr").click(function(){
   $(this).toggleClass('selected');
});

基本上,您創建一個類並從所選行中添加/刪除它。

順便說一句,您本可以付出更多努力,您的代碼中根本沒有 css 或 jquery/js xD

jQuery :

$("#data td").toggle(function(){
    $(this).css('background-color','blue')
},function(){
    $(this).css('background-color','ur_default_color')
});

刪除表的第二個 id 聲明:

<table id="data" border="1" cellspacing="1" width="500" **id="table1"**>

我不是 JQuery 的專家,但我有同樣的場景,我能夠像這樣完成:

$("#data tr").click(function(){
   $(this).addClass("selected").siblings().removeClass("selected"); 
});

風格:

<style type="text/css">

.selected {
    background: red;
}

</style> 

.highlight { 背景色:木瓜; }

$("#table tr").click(function() {    
 $("#table tr").removeClass("highlight");
            $(this).addClass("highlight");
});

設置背景色

.highlight { background-color: red; }

只選擇一行

 $("#Table_Id tr").click(function () {
        $("#Table_Id tr").removeClass("highlight");
        $(this).addClass("highlight");
    });

用於選擇多行

 $("#Table_Id tr").click(function () {
        $(this).addClass("highlight");
    });

要更改單元格的顏色:

$(document).on('click', '#table tbody td', function (event) {


    var selected = $(this).hasClass("obstacle");
    $("#table tbody td").removeClass("obstacle");
    if (!selected)
        $(this).addClass("obstacle");

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM