gpt4 book ai didi

javascript - 单击更改类别

转载 作者:行者123 更新时间:2023-11-27 22:32:00 25 4
gpt4 key购买 nike

我想在第一次单击时添加类up,在第二次单击时添加类down,然后在第三次单击时删除所有类。

这是我的代码,但类没有按各自的顺序应用:

function initAdClass() {
var trigger = '.post-info h2';
jQuery(trigger).click(function(e) {
if ( jQuery(this).not('.up') && jQuery(this).not('.down') ) {
jQuery(this).addClass('up');
}
else if ( jQuery(this).is('.up') && jQuery(this).not('.down') ) {
jQuery(this).removeClass('up').addClass('down');
}
else if ( jQuery(this).not('.up') && jQuery(this).is('.down') ) {
jQuery(this).removeClass('down');
}
});
}

最佳答案

试试这个片段。

var clicks=0;
$('a').click(function(){

clicks = !isNaN($(this).attr('id')) ? $(this).attr('id') + 1 : 0;
if(clicks == 1)
{
$(this).addClass('Up').html('Up added');
}
if(clicks == 2)
{
$(this).removeClass('Up').addClass('Down').html('Up removed and Down Added');
}
if(clicks == 3)
{
$(this).removeClass().html('All removed');
clicks=0;
}
$(this).attr('id', clicks);
$(this).append(" " + clicks);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:void(0)">Trigger : 1 : </a>

<br/><br/>

<a href="javascript:void(0)">Trigger : 2 : </a>

<br/><br/>

<a href="javascript:void(0)">Trigger : 3 : </a>

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

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