我想從頭開始做一個滾動視圖,其上有5個項目,我使用div標籤爲這些項目結合jQuery和CSS類。jQuery切換div類上點擊不工作
點擊事件正在觸發,但由於某種原因未設置新類(圓應該是白色),並且它恢復爲其默認顏色。
我使用引導3和最新的jQuery
HTML
<div class="row-fluid">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="circle 1" id="circle_1>
</div>
</div>
<div class="col-md-2">
<div class="circle 2" id="circle_2">
</div>
</div>
<div class="col-md-2">
<div class="circle 3" id="circle_3">
</div>
</div>
<div class="col-md-2">
<div class="circle 4" id="circle_4">
</div>
</div>
<div class="col-md-2">
<div class="circle 5" id="circle_5">
</div>
</div>
<div class="col-md-1"></div>
</div>
CSS
/* CSS used here will be applied after bootstrap.css */
.circle-selected{
border-radius: 50%;
width: 100px;
height: 100px;
background: #ffffff;
cursor: pointer;
}
.circle {
border-radius: 50%;
width: 100px;
height: 100px;
background: #949494;
-webkit-transition: all ease .3s;
-moz-transition: all ease .3s;
-o-transition: all ease .3s;
transition: all ease .3s;
font-family: 'Corbel Bold';
/* width and height can be anything, as long as they're equal */
}
.circle:hover{
border-radius: 50%;
width: 100px;
height: 100px;
background: #ffffff;
cursor: pointer;
}
jQuery的
$(".circle").click(function(){
var ID = $(this).attr('class').replace('circle ', '');
//alert('called 1');
$("#circle_" + ID).addClass('circle ' + ID).removeClass('circle-selected ' + ID);
});
$(".circle-selected").click(function(){
var ID = $(this).attr('class').replace('circle-selected ', '');
//alert('called 2');
$("#circle_" + ID).addClass('circle-selected ' + ID).removeClass('circle ' + ID);
});
例子是最好的所以這裏是我的:http://www.bootply.com/6qrSq5KvkK
有人可以指出我在這裏做錯了什麼?我試圖.switchClass,它不工作,所以通過這樣搜索,我發現這裏jQuery UI switchClass() method is not working properly暗示的答案,我應該更換
.switchClass
隨着
addClass('square').removeClass('circle');
1)爲什麼在ID值中使用「空格」?嘗試用下劃線替換空格。 2)嘗試先刪除課程,然後再添加其他課程。 –