我的代码遇到问题。 我想在单击按钮时更改按钮的颜色。 您可以检查下面给出的我的代码。
<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/