Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 点击改变课程_Javascript_Jquery_Html - Fatal编程技术网

Javascript 点击改变课程

Javascript 点击改变课程,javascript,jquery,html,Javascript,Jquery,Html,我想在第一次单击时添加类向上,第二次单击时添加类向下,第三次单击时删除所有类 这是我的代码,但类不是按各自的顺序应用的: function initAdClass() { var trigger = '.post-info h2'; jQuery(trigger).click(function(e) { if ( jQuery(this).not('.up') && jQuery(this).not('.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点击次数=0;
$('a')。单击(函数(){
单击=!isNaN($(this.attr('id'))?$(this.attr('id'))+1:0;
如果(单击==1)
{
$(this.addClass('Up').html('Up added');
} 
如果(单击==2)
{
$(this).removeClass('Up').addClass('Down').html('Up removed and Down Added');
} 
如果(单击==3)
{
$(this.removeClass().html('All removed');
点击次数=0;
}
$(this.attr('id',单击);
$(this.append(“+”点击次数);
});






尝试将
not
更改为
hasClass
@Shahnawa Khalid您尝试过我的答案吗?它工作正常,但存在一个错误,如果我们有两个或两个以上的触发器,那么需要从其他触发器中删除该类,并在“this”触发器上添加类,“clicks”应该根据单击的触发器计数。目前点击次数的计算是基于通用触发器,再添加2个触发器,您就会看到计数错误。@ShahnawazKhalid显示您的HTML,您到底想实现什么。@ShahnawazKhalid我已经编辑了我的答案,这是您要找的吗?