javascript - 单击时更改所有按钮的颜色

标签 javascript jquery html css onclick

发生了什么:

只有第一个按钮的颜色被改变。单击任何其他按钮时它会发生变化。

应该发生什么:

单击该特定按钮时,按钮的颜色应该会发生变化。

我犯了什么错误,想不通。

/*search-box*/

$('#click').click(function()
{   
    $("#search-box").toggle();     
});


/*seat-color*/
 var count = 1;
    function setColor(btn, color) {
        var property = document.getElementById(btn);
        if (count == 0) {
            property.style.backgroundColor = "#FFC0CB"
            count = 1;        
        }
        else {
            property.style.backgroundColor = "#800080"
            count = 0;
        }
    }
	#search-box{
	height: 600px;
    width: 600px;
    background:red;
    display: none;
    font-size: xx-large;

	}
	
    #button{
	height: 20px;
    width: 40px;
    background:pink;	
	}
<! doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

	
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" >

<!-- Custom CSS -->
    <link href="custom_css/main.css" rel="stylesheet" type='text/css'>  

</head>  
  
<body>


<button type="submit" class="btn btn-default btn-success" id="click" value="Show/Hide"><span class="glyphicon glyphicon-off"></span>Search</button>
 
	   <div id="search-box">
    <table>
  <tr>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
	 <td><input type="button" id="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>
  <tr>
     <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
	 <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>
  
  <tr>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
	<td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>
  <tr>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink"onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
	<td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" id="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>

  </tr>
</table>
</div>
  
 
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	
    	
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>

<!--Custome Jquery-->
<script src="custom_js/main.js"></script>


</body>

</html>	 

最佳答案

使用 JQuery:

<html>
    <title>Demo</title>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<style>

</style>
</head>
<body>

<button type="submit" class="btn btn-default btn-success" id="click" value="Show/Hide"><span class="glyphicon glyphicon-off"></span>Search</button>

       <div id="search-box">
    <table>
  <tr>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button" style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>
  <tr>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
     <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>

  <tr>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
  </tr>
  <tr>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink"onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>
    <td><input type="button" class="button"  style="margin:10px;" "color:pink" onclick="setColor('button', '#101010');"/></td>

  </tr>
</table>
</div>



    <script type="text/javascript">
    // Set firstime color if needed
    toogleColor();     

    $('#click').click(function() { toogleColor(); });
    function toogleColor() {
        $('.button').each(function(el) { 
            console.log('coucou');
            this.style.backgroundColor = this.style.backgroundColor !== "blue"
                ? "blue"
                : "red";
        });
    }
    </script>
    </body>
</html>

关于javascript - 单击时更改所有按钮的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085946/

相关文章:

javascript - 如何以选项卡形式发送多个单选按钮值

javascript - 从 jquery 的下拉列表中提取值只需要数值

javascript - 如何在不使用 jquery 的情况下使用 Angular 在 div 中添加内容

html - 调整页面大小时如何使元素保持相同的位置?

html - 在 input 元素的内容上应用 CSS 样式

javascript - js 如何从 $(document) 和 $(window) 获取选择器?

Javascript,解析变量中的多个值

javascript - mirth javascript 翻译返回错误

javascript - 当选中复选框时,如何动态地将复选框值从 View 传递到 Controller ?

html - HTML 表格内的输入文本溢出