jquery - I cannot remove the class "arrow-open" when clicked on the opened arrow again
问题描述
I need to add the class "arrow-open" on after the click on "has-child". and remove when clicked on other siblings of "has-child". It works till that but doesn't remove it, when I click again on the active "has-child".
$('.has-child').click(function() {
$(this).toggleClass('open-child');
$(this).siblings().removeClass('open-child');
// remove classes from all
$('.fa-angle-right').removeClass('arrow-open');
// add class to the one we clicked
$('.fa-angle-right', this).addBack(this).toggleClass('arrow-open');
});
This is the html
<div id="mobile-menu">
<ul class="mobile-nav">
<li><a href="index.php">Home</a></li>
<li class="has-child"><a href="#">Platform <i class="fas fa-angle-right"></i></a>
<ul class="child-nav">
<li><a href="hr.php">HR</a></li>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
</ul>
</li>
<li class="has-child"><a href="#">Platform <i class="fas fa-angle-right"></i></a>
<ul class="child-nav">
<li><a href="hr.php">HR</a></li>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
</ul>
</li>
<li class="has-child"><a href="#">Platform <i class="fas fa-angle-right"></i></a>
<ul class="child-nav">
<li><a href="hr.php">HR</a></li>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
</ul>
</li>
</ul>
解决方案
更改您的脚本功能的顺序,例如
$('.has-child').click(function() {
$(this).siblings().removeClass('open-child');
$(this).siblings().removeClass('arrow-open');
$(this).toggleClass('open-child');
// remove classes from all
// add class to the one we clicked
$('.fa-angle-right', this).addBack(this).toggleClass('arrow-open');
});
推荐阅读
- arrays - 如何访问这个多维 json 数组中的数据?
- java - 为什么大型 RSA 密钥不加密为唯一值?
- machine-learning - 如何根据某人之前看过的电影推荐电影?
- javascript - 如何使用 D3 设置出现在下拉列表中的默认值?
- javascript - 当我在控制台中运行 dispatch 方法时,为什么我的操作创建者会返回 ReferenceError 消息?
- mysql - 在 MySQL 存储过程中序列化两个顺序事务
- php - ODBC PDO 选择查询 LIKE 子句在 PHP 7 中不起作用
- vb.net - 使用 Form.KeyPreview 将 KeyPress 传递到文本框
- java - 表格分页和按列过滤 spring mvc + thymeleaf + bootstrap
- javascript - 用于合并值列表/数组的 BigQuery JavaScript UDF