按钮外观未在类更改时更新

时间:2017-08-20 09:49:31

标签: javascript html css button

单击按钮时,其背景颜色不会按预期变为黄色,也不会将之前单击的按钮的背景颜色恢复为红色。我的目标是改变按钮'通过更改类来单击它们时的颜色。当按钮'课程被改变,他们不改变颜色。 testFunction()的目的是更新按钮的颜色。 testFunction2()的目的是为按钮添加onclick函数。

<!DOCTYPE html>
<html>

<head>
    <title>TEST</title>
</head>
<style>
    .button {
        background-color: red;
    }

    .c {
        background-color: yellow;
    }
</style>

<body>
    <table>
        <tr>
            <td>test1</td>
        </tr>
        <tr>
            <td id="test1">
                <button class="c">0</button>
                <button class="">1</button>
                <button class="">2</button>
                <button class="">3</button>
            </td>
        </tr>
        <tr>
            <td>test2</td>
        </tr>
        <tr>
            <td id="test2">
                <button class="c">0</button>
                <button>1</button>
                <button>2</button>
                <button>3</button>
            </td>
        </tr>
    </table>

    <script>
        var lastClick1 = 0;
        var lastClick2 = 0;
        //alert('js');
        function testFunction(c, n) {
            //  alert('tf');
            if (c == 0) {
                //  alert('s=0');
                if (lastClick1 != n) {
                    var a = document.getElementById("test1").children;
                    a[lastClick1].class = '';
                    a[n].class = 'c';
                    alert('n: ' + n + ' class: ' + a[n].class);
                    alert('lastclick:' + lastClick1 + ' class: ' + a[lastClick1].class);
                    lastClick1 = n;
                }
            } else {
                //  alert('else');
                if (lastClick2 != n) {
                    var b = document.getElementById("test2").children;
                    b[lastClick2].class = '';
                    b[n].class = 'c';
                    alert('n: ' + n + ' class: ' + b[n].class);
                    alert('lastclick:' + lastClick2 + ' class: ' + b[lastClick2].class);
                    lastClick2 = n;
                }
            }
        }

        function testFunction2() {
            //alert('tf2');
            var t1 = document.getElementById("test1").children;
            var t2 = document.getElementById("test2").children;
            //alert(t1);
            for (var i = 0; i < 6; i++) {
                t1[i].onclick = function() {
                    testFunction(0, parseInt(this.innerText));
                };
                t2[i].onclick = function() {
                    testFunction(1, parseInt(this.innerText));
                };
            }
        }

        testFunction2();
    </script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

添加到jQuery

link

CSS

.button {
    background-color: red;
}

.c {
    background-color: yellow;
}
.r{
    background-color: red;
}

Jquery的

$(function(){
    $('button').on('click', function () {
        $(this).parent('td').children('.c').removeClass('c').addClass('r');

        $(this).addClass('c');
    })
});