我写了下面的代码行
$(document).ready(function()
{
$('.btn-custom').click(function() {
var id = $(this).attr('id');
$('.btn-custom').addClass('.btn-custom-primary');
$("#"+id).addClass('btn-custom-selected');
});
...
});
这些自定义按钮的html在浏览器中呈现为
<div id="rooms-information" class="form-group">
<div id="3ccd3803-a8ba-0328-cf9d-28bb72abbcae" class="btn-custom btn-custom-primary col-md-4 room-no-6"><div class="room-info-section"><span>Room No.: 6</span><br><span id="room0">Deluxe</span></div><div class="capacity-info"><span>Total Capacity: 8</span><br><span>Available: 8</span></div></div>
<div id="4908ec27-b2ca-7bf7-de3d-83181d5c41a6" class="btn-custom btn-custom-primary col-md-4 room-no-1"><div class="room-info-section"><span>Room No.: 1</span><br><span id="room1">Deluxe</span></div><div class="capacity-info"><span>Total Capacity: 4</span><br><span>Available: 4</span></div></div>
<div id="531709a7-6bc2-ddbe-3f3d-1642b7d70929" class="btn-custom btn-custom-primary col-md-4 room-no-3"><div class="room-info-section"><span>Room No.: 3</span><br><span id="room2">Simple</span></div><div class="capacity-info"><span>Total Capacity: 4</span><br><span>Available: 4</span></div></div>
<div id="cac24d40-0e85-6707-9797-5f9f3449ecf8" class="btn-custom btn-custom-primary col-md-4 room-no-4"><div class="room-info-section"><span>Room No.: 4</span><br><span id="room3">Deluxe</span></div><div class="capacity-info"><span>Total Capacity: 6</span><br><span>Available: 6</span></div></div>
</div>
我想更改选中或点击的div类的名称,div的其余部分应包含原始类&#34; btn-custom-primary&#34;。 上面的代码不起作用......请帮忙!!!
答案 0 :(得分:4)
这是你想要的吗?
$(document).ready(function() {
$('.btn-custom').click(function() {
console.log("test");
var id = $(this).attr('id');
$('.btn-custom').removeClass('btn-custom-selected').addClass("btn-custom-primary");
$(this).removeClass("btn-custom-primary").addClass('btn-custom-selected');
});
});
<强>演示强>
$(document).ready(function() {
$('.btn-custom').click(function() {
var id = $(this).attr('id');
$('.btn-custom').removeClass('btn-custom-selected').addClass("btn-custom-primary");
$(this).removeClass("btn-custom-primary").addClass('btn-custom-selected');
});
});
.btn-custom-selected {
color: blue
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rooms-information" class="form-group">
<div id="3ccd3803-a8ba-0328-cf9d-28bb72abbcae" class="btn-custom btn-custom-primary col-md-4 room-no-6">
<div class="room-info-section"><span>Room No.: 6</span><br><span id="room0">Deluxe</span></div>
<div class="capacity-info"><span>Total Capacity: 8</span><br><span>Available: 8</span></div>
</div>
<div id="4908ec27-b2ca-7bf7-de3d-83181d5c41a6" class="btn-custom btn-custom-primary col-md-4 room-no-1">
<div class="room-info-section"><span>Room No.: 1</span><br><span id="room1">Deluxe</span></div>
<div class="capacity-info"><span>Total Capacity: 4</span><br><span>Available: 4</span></div>
</div>
<div id="531709a7-6bc2-ddbe-3f3d-1642b7d70929" class="btn-custom btn-custom-primary col-md-4 room-no-3">
<div class="room-info-section"><span>Room No.: 3</span><br><span id="room2">Simple</span></div>
<div class="capacity-info"><span>Total Capacity: 4</span><br><span>Available: 4</span></div>
</div>
<div id="cac24d40-0e85-6707-9797-5f9f3449ecf8" class="btn-custom btn-custom-primary col-md-4 room-no-4">
<div class="room-info-section"><span>Room No.: 4</span><br><span id="room3">Deluxe</span></div>
<div class="capacity-info"><span>Total Capacity: 6</span><br><span>Available: 6</span></div>
</div>
</div>
答案 1 :(得分:0)
你所做的远不止于此。单击某个元素时,您将在jQuery中的处理函数中使用$(this)
引用它。在那里你可以为特定的点击元素添加你想要的任何类。
所有其他div已经有了btn-custom-primary
个班级。所以你不需要第二次添加它
$('.btn-custom').click(function() {
$(this).removeClass('btn-custom-primary').addClass('btn-custom-selected');
});