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

JQuery Change TR background Colour depending on currently selected Text Input

本文关键字:选择 文本 输入 取决于 改变 TR 背景 颜色 JQuery      更新时间:2023-09-26

我试图使一个系统,改变当前输入框正在输入/选择的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元素的颜色不同。

提前感谢,抱歉我的解释不好!

Try

$('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
});