如下图所示,当我点击“>”符号的时候,由向下变成向上,我再点击其他的符号的时候,要点击两次才能将“>”变成向上,以下是我的代码:

代码:
排名
页面名称
会话发起次数
发起会话人数
会话率
JS:
var flag = false;
function sessionSort(e) {
// $(e).parent().siblings().children('span').children('i').removeClass('downArow');
console.log($(e).parent().siblings().children('span').children()[0],112);
if (flag){
// console.log($(e).children('i').eq(0)[0].className,555)
$(e).children('i').removeClass('upArrow').addClass('downArow');
} else {
$(e).children('i').removeClass('downArow').addClass('upArrow');
$(e).children('i').attr('class') == 'upArrow'
? $(e).parent().siblings().children('span').children('i').removeClass('upArrow').addClass('downArow')
:' ';
}
console.log( $(e).children('i').attr('class') == 'downArow',1231)
flag = !flag;
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
$('.thd').on('click', '.sort', function () {
因为大家共用一个
flag一步步来
flag
false->true(由于flag = !flag;)第一次
由于
flag为true,进入第一个分支$(e).children('i').removeClass('upArrow').addClass('downArow');仍然是
downArow状态然后
flag = !flag;flag 变为false第二次 才是真正执行你想要的结果
ps: arow/arrow 混用可不好
关于解决方法 我想到一种: 把状态存到一个对象里,使用前先查找是否存在这个状态
通过制造一个每个点击都不一样的 key (我这里取了点击节点的文本)
简单做法 on绑定用this这样保持独立性
HTML:
JS:
补充了下,在“up”分支下加了句整体重置的代码,线上版本