gpt4 book ai didi

javascript - 使用 jQuery 应用 if 和 else 条件添加和删除类

转载 作者:行者123 更新时间:2023-11-28 15:06:44 25 4
gpt4 key购买 nike

我创建了下面的代码,我想创建一个 if/else 条件,当用户单击标签时,将添加一个类,当取消单击时,该类将被删除。我也坚持应用同样的方法。

我的代码如下。

现场 fiddle 链接

<强> Complete working code here

HTML

<div class="customCheckboxBtns">
<div class="ck-button"><label><input checked="checked" name="column-2" type="checkbox">1</label></div>
<div class="ck-button"><label class="selected"><input checked="checked" name="column-3" type="checkbox">2</label></div><!-- This is how it will be added dynamically -->
<div class="ck-button"><label><input checked="checked" name="column-4" type="checkbox">3</label></div>
<div class="ck-button"><label><input checked="checked" name="column-5" type="checkbox">4</label></div>
<div class="ck-button"><label><input checked="checked" name="column-6" type="checkbox">5</label></div>
</div>

CSS

.customCheckboxBtns{
margin-bottom: 1rem;
margin-top: 0.25rem;
overflow:auto;
}

.ck-button {
margin-right: 0.375rem;
background-color:#ffffff;
border-radius:4px;
border:1px solid #26a69a;
overflow:auto;
float:left;
font-family: 'Source Sans Pro', sans-serif;
transition: all 0.2s ease 0s;
color:#999999;
}

.ck-button:hover {
background:#7dcac2;
border:1px solid #26a69a;
color:#26a69a;
background-color:#ffffff;
}

.ck-button label {
text-align:center;
padding: 0.25rem 0.5rem;
display:block;
background:#26a69a;
color:#ffffff;
transition: all 0.2s ease 0s;
}
.ck-button label:hover{
cursor:pointer;
}
.ck-button input:checked {
background: #26a69a none repeat scroll 0 0;
color: #ffffff;
padding: 0.25rem 0.5rem;
transition: all 0.2s ease 0s;
}
.ck-button label:hover{
background: #7dcac2 none repeat scroll 0 0;
color:#ffffff;
}
.ck-button label input {
position:absolute;
/*top:-2000px;*/
}
.ck-button label input[type="checkbox"] {
position: absolute;
visibility: hidden;
}
label.selected{
background: #7dcac2 none repeat scroll 0 0;
color:#ffffff;
}

脚本

$('.ck-button label').on('click', function () {
$(this).addClass('selected');
});

$('.ck-button label').on('click', function () {
$(this).removeClass('selected');
});

最佳答案

由于您正在尝试复制复选框行为,我可能会使用类似的东西:

$('.ck-button input').on('change', function() {
$(this.parentNode).toggleClass('selected', this.checked);
});
.customCheckboxBtns {
margin-bottom: 1rem;
margin-top: 0.25rem;
overflow: auto;
}

.ck-button {
margin-right: 0.375rem;
background-color: #ffffff;
border-radius: 4px;
border: 1px solid #26a69a;
overflow: auto;
float: left;
font-family: 'Source Sans Pro', sans-serif;
transition: all 0.2s ease 0s;
color: #999999;
}

.ck-button:hover {
background: #7dcac2;
border: 1px solid #26a69a;
color: #26a69a;
background-color: #ffffff;
}

.ck-button label {
text-align: center;
padding: 0.25rem 0.5rem;
display: block;
background: #26a69a;
color: #ffffff;
transition: all 0.2s ease 0s;
}

.ck-button label:hover {
cursor: pointer;
}

.ck-button input:checked {
background: #26a69a none repeat scroll 0 0;
color: #ffffff;
padding: 0.25rem 0.5rem;
transition: all 0.2s ease 0s;
}

.ck-button label:hover {
background: #7dcac2 none repeat scroll 0 0;
color: #ffffff;
}

.ck-button label input {
position: absolute;
/*top:-2000px;*/
}

.ck-button label input[type="checkbox"] {
position: absolute;
visibility: hidden;
}

label.selected {
background: #7dcac2 none repeat scroll 0 0;
color: #ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="customCheckboxBtns">
<div class="ck-button">
<label>
<input name="column-2" type="checkbox">1</label>
</div>
<div class="ck-button">
<label class="selected">
<input checked="checked" name="column-3" type="checkbox">2</label>
</div>
<div class="ck-button">
<label>
<input name="column-4" type="checkbox">3</label>
</div>
<div class="ck-button">
<label>
<input name="column-5" type="checkbox">4</label>
</div>
<div class="ck-button">
<label>
<input name="column-6" type="checkbox">5</label>
</div>
</div>

JSFiddle: https://jsfiddle.net/zdwjnLvL/15/

关于javascript - 使用 jQuery 应用 if 和 else 条件添加和删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38608053/

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