2017-07-15 103 views
0

這裏我創建了4個按鈕。點擊後,它應該改變顏色。我想要的是,當我點擊第二個按鈕時,我想要第一個按鈕以及第二個按鈕被着色。我該怎麼做?點擊第二個按鈕,我想要第一個以及第二個按鈕變得有顏色

<?php 
 
session_start(); 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<style> 
 
.button { 
 
    
 
    background-color: white; 
 
    border: 1px solid black; 
 
    color: white; 
 
    padding: 8px 30px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    cursor: pointer; 
 
    float:left; 
 
} 
 
</style> 
 
</head> 
 

 
<body> 
 
<input type="button" class="button" onclick="this.style.backgroundColor = 'red';" value="1"> 
 
<input type="button" class="button" onclick="this.style.backgroundColor = 'yellow';" value="2"> 
 
<input type="button" class="button" onclick="this.style.backgroundColor = 'green';" value="3"> 
 
<input type="button" class="button" onclick="this.style.backgroundColor = 'orange';" value="4"> 
 
</body> 
 
</html>

+0

哪裏是你的JavaScript?這和php有什麼關係? – FKEinternet

+0

刪除php標籤,php將無法幫助,因爲它是服務器端。 –

+0

@NoahCristino我相信你的意思是客戶端(在用戶的瀏覽器中) - 或者等待 - 你的意思是* PHP *是服務器端? – FKEinternet

回答

1

querySelector()。需要觸發點擊第一個按鈕事件試穿過程中第二個按鈕點擊

更新

  1. 增添了不少色彩untill點擊的元素

$('.button').click(function() { 
 
    var that =this; 
 
    $('.button').each(function(){ 
 
    $(this).css('background-color', $(this).attr('data-color')); 
 
    if(that == this){ 
 
    return false; 
 
    } 
 
    }) 
 
})
.button { 
 
    background-color: white; 
 
    border: 1px solid black; 
 
    color: white; 
 
    padding: 8px 30px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    cursor: pointer; 
 
    float: left; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
    <input type="button" class="button" data-color="red" value="1"> 
 
    <input type="button" class="button" data-color="yellow" value="2"> 
 
    <input type="button" class="button" data-color="green" value="3"> 
 
    <input type="button" class="button" data-color="orange" value="4"> 
 
</body>

+0

同樣,如果我點擊3按鈕,我想第一,第二,第三按鈕得到coloured.And通過點擊第四按鈕,我希望所有的按鈕得到coloured.how我可不可以做??? –

+0

@PriyalPatel查看我更新的答案 – prasanth

相關問題