Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我该如何改变<;tr>;使用复选框使用Javascript的bgcolor?_Javascript_Jquery_Html_Css_Background Color - Fatal编程技术网

我该如何改变<;tr>;使用复选框使用Javascript的bgcolor?

我该如何改变<;tr>;使用复选框使用Javascript的bgcolor?,javascript,jquery,html,css,background-color,Javascript,Jquery,Html,Css,Background Color,我有这个html <table border="1"> <tr> <td> <input type="checkbox"> </td> <td> Hello </td> <td> my friend! </td>

我有这个html

<table border="1">
    <tr>
        <td>
            <input type="checkbox">
        </td>
        <td>
            Hello
        </td>
        <td>
            my friend!
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox">
        </td>
        <td>
            ...
        </td>
        <td>
            ...
        </td>
    </tr>
    ...
</table>

你好
我的朋友!
...
...
...
当复选框被激活时,如果我想更改我的
bgcolor,我需要做什么?我想我可能需要用
onclick()
做点什么

编辑:我需要的是复选框只修改当前行。

CSS:


可能的方法之一是:

document.getElementById('table').onclick = function(e) {
    var e = e || event,
        el = e.srcElement || e.target;
    if (el.tagName == 'INPUT') {
        el.parentNode.parentNode.className = el.checked ? 'active' : '';
    }
};


在本例中,事件冒泡到侦听它的父表,然后在需要时将其分派。

如果将复选框移到表外,也可以使用纯css执行此操作:@Fabriziocarderan我想最后一个表中不会只有一个复选框。实际上,我需要在表内使用它,并且我只需要“当前”复选框要修改的选定行。@a是否打开以使用库?e、 g.?这绝对没有必要……如果OP没有将其包含在问题或问题标签中,我会小心地建议jQuery。(投反对票的不是我)
tr.active {
    background-color: #FFC;
}
document.getElementById('table').onclick = function(e) {
    var e = e || event,
        el = e.srcElement || e.target;
    if (el.tagName == 'INPUT') {
        el.parentNode.parentNode.className = el.checked ? 'active' : '';
    }
};