gpt4 book ai didi

javascript - jQuery 添加一个类但不会删除一个

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:20 25 4
gpt4 key购买 nike

当您单击 .extra anchor 时,它会添加“extra-active”类并删除“extra”类,但是当您单击 extra-active anchor 时,它拒绝删除 extra-active 类并将其替换为 extra .

这是 HTML

	$("a.extra").click(function(e){
$(this).addClass("extra-active").removeClass("extra");
e.preventDefault();
})

$("a.extra-active").click(function(e){
$(this).removeClass("extra-active").addClass("extra");
e.preventDefault();
})
.extra{
display:inline-block;
height: 128px;
width: 128px;

background-color:grey;
padding:12px;
border-radius:8px;
}

.extra-group{
p {
text-align:center;
}
}


.extra:hover{
background-color:blue;
}

.extra-active{
display:inline-block;
height: 128px;
width: 128px;

background-color:red !important;
padding:12px;
border-radius:8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="display:inline-block;">
<span class="extra-group">
<a class="extra" href="" title = "An hour of Ironing" id="extra-ironing" style = "background-image: url('{{ url_for('static', filename='book-icons/iron-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>An Hours Ironing</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "Inside the Fridge" id="extra-fridge" style = "background-image: url('{{ url_for('static', filename='book-icons/fridge-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>Inside the Fridge</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "Inside the Oven" id="extra-oven" style = "background-image: url('{{ url_for('static', filename='book-icons/oven-icon.png') }}'); background-size: 80% 80%;; background-repeat: no-repeat; background-position: center center;"></a>
<p>Inside the Oven</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "A load of Washing" id="extra-washing" style = "background-image: url('{{ url_for('static', filename='book-icons/washer-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>A load of Washing</p>
</span>
<span class="extra-group">
<a class="extra " href="" title = "Interior Windows" id="extra-windows" style = "background-image: url('{{ url_for('static', filename='book-icons/window-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>Interior Windows</p>
</span>
</span>

最佳答案

扩展@xandercoded 的贡献,我向主体添加了一个监听器,然后更新了 on 函数的目标。

	$("body").on('click','.extra',function(e){
$(e.target).addClass("extra-active").removeClass("extra");
e.preventDefault();
})

$("body").on('click','.extra-active',function(e){
$(e.target).removeClass("extra-active").addClass("extra");
e.preventDefault();
})
.extra{
display:inline-block;
height: 128px;
width: 128px;

background-color:grey;
padding:12px;
border-radius:8px;
}

.extra-group{
p {
text-align:center;
}
}


.extra:hover{
background-color:blue;
}

.extra-active{
display:inline-block;
height: 128px;
width: 128px;

background-color:red !important;
padding:12px;
border-radius:8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="display:inline-block;">
<span class="extra-group">
<a class="extra" href="" title = "An hour of Ironing" id="extra-ironing" style = "background-image: url('{{ url_for('static', filename='book-icons/iron-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>An Hours Ironing</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "Inside the Fridge" id="extra-fridge" style = "background-image: url('{{ url_for('static', filename='book-icons/fridge-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>Inside the Fridge</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "Inside the Oven" id="extra-oven" style = "background-image: url('{{ url_for('static', filename='book-icons/oven-icon.png') }}'); background-size: 80% 80%;; background-repeat: no-repeat; background-position: center center;"></a>
<p>Inside the Oven</p>
</span>
<span class="extra-group">
<a class="extra" href="" title = "A load of Washing" id="extra-washing" style = "background-image: url('{{ url_for('static', filename='book-icons/washer-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>A load of Washing</p>
</span>
<span class="extra-group">
<a class="extra " href="" title = "Interior Windows" id="extra-windows" style = "background-image: url('{{ url_for('static', filename='book-icons/window-icon.png') }}'); background-size: 80% 80%; background-repeat: no-repeat; background-position: center center;"></a>
<p>Interior Windows</p>
</span>
</span>

关于javascript - jQuery 添加一个类但不会删除一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710827/

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