gpt4 book ai didi

javascript - 使用 JQuery 在图标类之间切换

转载 作者:行者123 更新时间:2023-12-03 06:49:34 25 4
gpt4 key购买 nike

我正在尝试使用一些 JQuery/Javascript 在我用于表排序的图标之间切换。我可以隐藏所有 i 元素,但在专门针对要隐藏/显示的类时遇到了一些麻烦。

例如,这些是我的 HTML 表格标题

  <th>Order No &nbsp; <i id='col1' class='fa fa-sort'></i></th>
<th>Cust Name &nbsp; <i id='col2' class='fa fa-sort'></i></th>
<th>Cust ID &nbsp; <i id='col3' class='fa fa-sort'></i></th>
<th>Doc Date &nbsp; <i id='col4' class='fa fa-sort'></i></th>
<th>Upload Date &nbsp; <i id='col5' class='fa fa-sort'></i></th>
<th>Path</th>

我想做的具体是定位元素 id,例如 col1 等。然后仅在这 3 个类之间单击时切换类名称,具体取决于当时显示的类。

这是一个典型的例子:

<th>Order No <i id='col1' class='fa fa-sort'></i></th>

我通常会在以下三个类别之间切换。

  • fa fa-排序
  • fa fa-排序-asc
  • fa fa-sort-desc

脚本我发现的任何教程似乎都只针对元素 id。我已经尝试过类似的方法,但它不起作用。任何有关这方面的指导将不胜感激。

    $(document).ready(function(){

$("#col1").click(function(){
$(".fa fa-sort").hide();
$(".fa fa-sort-asc").show();
});

$("#col2").click(function(){
$(".fa fa-sort-asc").hide();
$(".fa fa-sort-desc").show();
});

$("#col3").click(function(){
$(".fa fa-sort-desc").hide();
$(".fa fa-sort").show();
});

});

看到 G 的建议后,我确​​实更改了代码,但它似乎不起作用。我将添加测试页代码,请原谅任何格式错误。

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<!-- CSS -->
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">

<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<style>

table
{
width: 90%;
margin: auto;margin-top: 20px;

border-spacing: 0;
border-collapse: collapse;

border: 1px solid #a0a0a0;
border-radius: 25px;
background-color: transparent;
}
.table
{
width: 90%;
margin-right: 5%;
margin-bottom: 20px;
margin-left: 5%;
}


th
{
padding: 10px;color: #fff;
background-color: #003466;
}

#col1, #col2, #col3, #col4, #col5
{
cursor:pointer;
}


tr
{
padding: 10px;
}
tr:nth-child(odd)
{
background-color: #fff;
}
tr:nth-child(even)
{
background-color: #efefef;
}
td
{
padding: 10px; text-align: center;
}
</style>
<script>
$("#col1").click(function() {
if($(this).hasClass('fa-sort')){
$(this).removeClass('fa-sort').addClass('fa-sort-asc');
return;
}
if($(this).hasClass('fa-sort-asc')){
$(this).removeClass('fa-sort-asc').addClass('fa-sort-desc');
return;
}
if($(this).hasClass('fa-sort-desc')){
$(this).removeClass('fa-sort-desc').addClass('fa-sort');
return;
}
});
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Order No &nbsp; <i id='col1' class='fa fa-sort'></i></th>
<th>Cust Name &nbsp; <i id='col2' class='fa fa-sort'></i></th>
<th>Cust ID &nbsp; <i id='col3' class='fa fa-sort'></i></th>
<th>Doc Date &nbsp; <i id='col4' class='fa fa-sort'></i></th>
<th>Upload Date &nbsp; <i id='col5' class='fa fa-sort'></i></th>
<th>Path</th>
</tr>
</thead>
</table>
</body>
</html>

最佳答案

根据上一个类的情况,切换到下一个类即可。请注意,如果没有“return”,则三个 if 将为 true。

$("#col1").click(function() {
if($(this).hasClass('fa-sort')){
$(this).removeClass('fa-sort').addClass('fa-sort-asc');
return;
}
if($(this).hasClass('fa-sort-asc')){
$(this).removeClass('fa-sort-asc').addClass('fa-sort-desc');
return;
}
if($(this).hasClass('fa-sort-desc')){
$(this).removeClass('fa-sort-desc').addClass('fa-sort');
return;
}
});

如果您要在很多类之间切换(我会为 5 个或更多类执行此操作),您应该创建一个函数。但在您的特定情况下,这效果很好。

关于javascript - 使用 JQuery 在图标类之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37566742/

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