所以我想讓按鈕點擊多個燈激活,我不知道我在這裏做錯了什麼。JavaScript新手試圖找出這
我想我可以把它做成一個函數,然後傳遞給它的id名稱,但它看起來不像我想要的那樣行事。
HTML
<div class="lights">
<div id="red"></div>
<div id="yellow"></div>
<div id="green"></div>
</div>
<div class="button">
<button id="red_button"> Red Button </button>
<button id="yellow_button">Yellow Button </button>
<button id="green_button">Green Button </button>
</div>
CSS
.lights{
height: 600px;
width: 200px;
background-color: black;
padding-top: 15px;
}
.button{
padding-top: 20px;
}
#red,
#yellow,
#green {
margin: 0 auto;
background-color: black;
border-radius: 50%;
width: 180px;
height: 180px;
margin-top: 10px;
}
#red.active {
background-color: red;
}
#yellow.active {
background-color: yellow;
}
#green.active {
background-color: green;
}
jQuery的
function click(e) {
$('#red,#yellow,#green').removeClass('active');
$('e').addClass('active');
}
$('#red_button').click(click('#red'));
$('#yellow_button').click(click('#yellow'));
$('#green_button').click(click('#green'));
http://jsfiddle.net/0m9wos1r/1/
要添加類項目與e'的'標籤,這似乎不正確... ...當你調用一個函數與傳遞的參數,你需要把它包在匿名功能。 – PlantTheIdea 2014-09-25 19:41:05
如果你更清楚地描述你要完成的工作,這將會有所幫助。這從代碼中看不出來。 – isherwood 2014-09-25 19:41:50
你傳入一個變量e,但將類添加到字符串'e'而不是(e).addClass - 儘管這裏可能會有更多錯誤。 – 2014-09-25 19:42:19