gpt4 book ai didi

javascript - 单击时更改 div 类的名称

转载 作者:行者123 更新时间:2023-11-28 14:34:05 25 4
gpt4 key购买 nike

我写了下面几行代码

        $(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 的其余部分应包含原始类“btn-custom-primary”。上面的代码不起作用...请帮助!!!

最佳答案

这是你想要的吗?

$(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>

关于javascript - 单击时更改 div 类的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com