JQuery更改TR背景颜色取决于当前选择的文本输入

时间:2014-03-22 17:32:05

标签: javascript jquery html css input

我正在尝试创建一个系统来更改当前输入框被输入/选中的TR元素的背景颜色。我目前拥有它,所以当在当前文本框中输入一个字母时,您将切换到下一个文本框。

<html>

<body>
    <table>
        <tr>
            <td>
                <input type='text' id='txt1' maxlength='1' />
            </td>
        </tr>
        <tr>
            <td>
                <input type='text' id='txt2' maxlength='1[' />
            </td>
        </tr>
    </table>
    <script src="Scouts/resources/plugins/jquery.js"></script>
    <script>
        $('#txt1').keyup(function () {
            if (this.value.length == $(this).attr('maxlength')) {
                $('#txt2').focus();
            }
        });
    </script>
</body>

</html>

我希望聚焦输入的父TR元素与其他TR元素的颜色不同。我希望根据重点输入进行更改。

提前致谢并对我糟糕的言论感到抱歉!

1 个答案:

答案 0 :(得分:2)

尝试

$('tr input').focus(function () {
    var $tr = $(this).closest('tr'); //get tr
    $tr.css('background-color', 'red'); //change background color 
    $tr.siblings().css('background-color', 'OldColor'); //set sibling tr's background color to be old color
}).blur(function () {
    var $tr = $(this).closest('tr');//get tr
    $tr.css('background-color', 'OldColor');//set tr's background color to be old color
});