javascript - 更改按钮中的 onclick 事件颜色

标签 javascript php jquery

我的代码遇到问题。 我想在单击按钮时更改按钮的颜色。 您可以检查下面给出的我的代码。

<html>
    <head>
    </head>
    <body onload="func();">
        <br><br><div id="header"><h1 align="center">Online Aptitude-2k17</h1></div>
        <table align="center" width="100%">
            <tr id="t1">
                <td></td>
            </tr>
        </table>

        <script>
            function func() {
                for(var i=1;i<=30;i++) {
                    var x=document.getElementById("t1").innerHTML;
                    var p="<td><input type='button' value='"+i+"' onclick ='myFunction("+i+");'/></td>";
                    document.getElementById("t1").innerHTML=x+p;
                }
            }

            function myFunction(a,elem) {
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) { 
                        document.getElementById("ques").innerHTML = this.responseText;

                    }
                };
                xhttp.open("GET", "retrieveQuestion.php?question=" +a, true);
                xhttp.send();
            }
        </script>

        <div id="ques" style="text-align:center; padding:20px"></div>
        <table align="center" cellspacing="20px">
            <tr><td align="center"><input type="text" name="answer" /></td></tr>
            <tr><td align="center"><input type="button" value="Submit" /></td></tr>
        </table>
      </body>
</html>

单行包含 30 列的按钮,我想要的是当用户单击它时,它必须在单击时更改它的颜色。

任何人都可以帮我解决这个问题吗?

最佳答案

将所有颜色放入数组

$("button").click(function(){
    var color = clicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicked = !clicked;
});

关于javascript - 更改按钮中的 onclick 事件颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591724/

相关文章:

javascript - 有什么方法可以在 DOM 修改后重新运行 jQuery 插件吗?

javascript - jquery show 隐藏请求的页面

javascript - 一个 <Router/> 只能有一个子元素

javascript - 避免重复 javascript 原型(prototype)定义 - 上下文问题

php - 从mysql获取第二行的输出

php - 如何在 MySQL 中的到期日期后更新列?

jQuery 验证错误类未从字段中删除

javascript - 如何加入一个数组,结果不是 Javascript 中的字符串?

javascript - 使用 react-router 使用react的 url 的问题

php - 具有可能连接的动态数据库组件