简体   繁体   English

jQuery更改TR背景颜色,具体取决于当前选择的文本输入

[英]JQuery Change TR background Colour depending on currently selected Text Input

I am trying to make a system that changes the background colour of the TR element which the current input box is being typed into / Selected . 我正在尝试制作一个更改TR元素的背景颜色的系统,该元素将当前输入框键入到/ Selected中。 I currently have it so you change to the next text box when a letter is entered into the current text box. 我目前拥有它,因此当在当前文本框中输入字母时,您将切换到下一个文本框。

<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>

I want the parent TR element of the focused input to be a different colour to the other TR elements. 我希望焦点输入的父TR元素与其他TR元素具有不同的颜色。 I want this to change depending on the focused input. 我希望根据关注的输入进行更改。

Thanks in advance and sorry for my bad explenation! 在此先感谢您,并感谢您的不满意!

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
});

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

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