gpt4 book ai didi

javascript - 为什么向按钮添加类仅在第二次单击后才有效?

转载 作者:行者123 更新时间:2023-12-03 07:07:50 25 4
gpt4 key购买 nike

当显示 bootstrap 折叠时和隐藏 bootstrap 折叠时,我尝试编辑按钮样式。但是当我第一次点击按钮时,会显示折叠,但按钮样式不会改变。当我第二次点击按钮时它们会改变(然后折叠被隐藏)

我的按钮:

<button type="button" class="btn-block" data-toggle="collapse" data-target="#demo1" id="demo1button"> 
Button
</button>

折叠元素

<div  class="collapse" id="demo1">
...
</div>

脚本

$("#demo1button").click(function(){
if(!$("#demo1").hasClass('show')){
$("#demo1button").addClass("collapsed");
}else{
$("#demo1button").removeClass("collapsed");
}
});

另外,我尝试用其他方式做到这一点,但它仍然不起作用

脚本

$("#demo1button").click(function(){
if($("#demo1button").hasClass('collapsed')){
$("#demo1button").removeClass("collapsed");
}else{
$("#demo1button").addClass("collapsed");
}
});

样式.css

.collapsed{
background-color: #7a7a7a;
}

最佳答案

我认为您不必使用外部 JS 来添加和删除类,因为您使用的是 bootstrap collapsible,因此 Bootstrap 将管理您的按钮元素的类列表。

你只需要在按钮 collapsed 中定义一个类,所以你的按钮应该是:

<button type="button" class="btn-block collapsed" data-toggle="collapse" data-target="#demo1" id="demo1button"> 
Button
</button>

然后以适当的方式应用您的事件内容 CSS。

这是我的代码片段中的演示代码:

.single_collapsible button {
color: #fff;
border: none;
outline: none;
background-color: green;
}

button.collapsed {
background-color: gray;
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
<div class="single_collapsible mb-2">
<button type="button" class="btn-block collapsed" data-toggle="collapse" data-target="#demo1" id="demo1button"> Button</button>
<div class="collapse" id="demo1">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<div class="single_collapsible mb-2">
<button type="button" class="btn-block collapsed" data-toggle="collapse" data-target="#demo2" id="demo2button"> Button</button>
<div class="collapse" id="demo2">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<div class="single_collapsible mb-2">
<button type="button" class="btn-block collapsed" data-toggle="collapse" data-target="#demo3" id="demo3button"> Button</button>
<div class="collapse" id="demo3">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
</div>
</body>

</html>

关于javascript - 为什么向按钮添加类仅在第二次单击后才有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64388244/

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