简体   繁体   English

如何更改表格中一行的颜色

[英]How to change color of a row in a table

I'm developing a table in html.我正在用html开发一个表格。 Every rows have a checkbox with the same attribute name as tag tr.每行都有一个与标签 tr 具有相同属性名称的复选框。 So I want to color the row selected in yellow if that raw is the unique selected, otherwise color in blue all the rows selected.因此,如果该 raw 是唯一选定的,我想将选定的行涂成黄色,否则将所有选定的行涂成蓝色。 So I was developing this:所以我正在开发这个:

var checked = $("input[@type=checkbox]:checked");  
                var nbChecked = checked.size(); 
                if(nbChecked==1){
                   var row = $('tr[name*="'+checked.attr("name")+'"]');
                   row.style.backgroundColor="#FFFF33";
                }

But color doesn't change :( can you tell me why? can you help me?但是颜色没有改变:(你能告诉我为什么吗?你能帮我吗?

<TR valign=top name="<?php echo $not[$j]['name'];?>">
        <TD width=12 style="background-color:#33CCCC">
        <div class="wpmd">
            <div align=center>
                <font color="#FF0000" class="ws7">
                    <input type="checkbox" name="<?php echo $not[$j]['name'];?>" onchange="analizeCheckBox()"/>
            </div>
.
.
.
.
.
.

Although this code doesn't get you all they way to the funcitonality, it does do the row updating.尽管此代码无法让您完全了解功能,但它确实进行了行更新。

http://jsfiddle.net/4QKe9/5/ http://jsfiddle.net/4QKe9/5/

HEre is the Javascript:这是Javascript:

function updateRows() {
    var checked = $("input:checked");
    var nbChecked = checked.size();
    if (nbChecked == 1) {
        checked.parent().parent().css("background", "#FFFF33");
    }
}

$(function () { $("input:checkbox").click(updateRows)});​

Firstly, you have got the background color on the cell not the row.首先,您在单元格而不是行上获得了背景颜色。 You need to move it.你需要移动它。

Here's your fixed html:这是您的固定html:

<table>
  <tr style="vertical-align:top; background-color:#33CCCC">
    <td style="width:12px">
      <div class="wpmd">
        <div class="ws7" style="text-align:center; color:#FF0000">
          <input type="checkbox" />
        </div>
      </div>
    </td>
  </tr>
</table>

and then this script will work然后这个脚本就可以工作了

$("td .ws7 input[type=checkbox]").bind({
  click: function()
  {
    if ($(this).is(":checked"))
    {
      $(this).closest("tr").css("background-color", "#ffff33");
    }
    else
    {
      $(this).closest("tr").css("background-color", "#33CCCC");
    }          
  }
});​

change the "td .ws7" selector to match your needs.更改“td .ws7”选择器以满足您的需要。

Example.例子。

When you call the jQuery function $() with a selector string it will return a jQuery object that is like an array even when only 1 or 0 elements matched .当您使用选择器字符串调用 jQuery 函数$()时,即使只有 1 或 0 个元素匹配,它也会返回一个类似于数组的 jQuery 对象。 So your code:所以你的代码:

row = $('tr[name*="'+checked.attr("name")+'"]');
row.style.backgroundColor="#FFFF33";

Doesn't work because row is not a DOM element with a style property, it's a jQuery object that's like an array with lots of extra methods.不起作用,因为row不是具有style属性的 DOM 元素,它是一个 jQuery 对象,就像一个包含许多额外方法的数组。 The individual DOM elements that matched can be accessed with array-style notation, so:可以使用数组样式表示法访问匹配的单个 DOM 元素,因此:

row[0].style.backgroundColor = "#FFFF33";

Will work to update the first matching DOM element.将更新第一个匹配的 DOM 元素。 If nothing matched then row[0] will be undefined and you'll get an error.如果没有匹配,则row[0]将未定义,您将收到错误消息。 row.length will tell you how many elements matched. row.length会告诉你有多少元素匹配。

In your code:在您的代码中:

var checked = $("input[@type=checkbox]:checked");

You don't need the @ symbol to match attribute names, so "input[type=checkbox]:checked" is OK, but "input:checkbox:checked" is simpler.您不需要@符号来匹配属性名称,因此"input[type=checkbox]:checked"可以,但"input:checkbox:checked"更简单。

So getting at last to your actual requirement, which is when a single row has a checked box to set that row's background to yellow, but if multiple rows have checked checkboxes to set all those rows' backgrounds to blue, you can do this with only three lines of code:因此,最终达到您的实际要求,即当单行有一个复选框以将该行的背景设置为黄色时,但如果多行已选中复选框以将所有这些行的背景设置为蓝色,您只能这样做三行代码:

// reset all trs to default color
$("tr").css("background-color", "yourdefaultcolorhere");
// select the checked checkboxes
var checked = $("tr input:checkbox:checked");
// set the checked checkboxes' parent tr elements' background-color
// according to how many there are
checked.closest("tr").css("background-color",
                          checked.length===1 ? "yellow" : "blue");

Notice that you don't need to select by the name attribute at all, because jQuery's .closest() method will let you find the tr elements that the checked checkboxes belong to.请注意,您根本不需要按name属性进行选择,因为 jQuery 的.closest()方法可以让您找到选中的复选框所属的 tr 元素。

WORKING DEMO: http://jsfiddle.net/FB9yA/工作演示:http: //jsfiddle.net/FB9yA/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM