Change class on inactive icon - jquery

2019-06-08 07:03发布

问题:

I have two icons in a header that have dropdown menus that are toggled when clicked.

Within each list item for each icon there is a 'i' tag which is Font Awesome for a down arrow. When each icon is clicked, the arrow changes to be pointing up by changing the class 'fa-chevron-down' to 'fa-chevron-up'.

However, if I click the help icon, the help dropwdown slides down and if I then click the basket icon, the help dropdown slides up and the basket dropdown slides down. The arrows in the relevant icon also need to change with this - the arrow on the help icon would need to change to point down and therefore the class needs to change back to 'fa-chevron-down' from 'fa-chevron-up'.

E.g. if the basket dropdown is active then the help icon 'i' tag should have the class 'fa-chevron-down' and the basket icon 'i' tag should have the class 'fa-chevron-up'. If the help dropdown is active then then basket icon 'i' tag should have the class 'fa-chevron-down' and the help icon 'i' tag should have the class 'fa-chevron-up'.

Here's a jsFiddle: http://jsfiddle.net/2srx4sv2/9/

The code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="format-detection=no,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,width=device-width" />
<title>Untitled Document</title>
<link href="http://www.swimmingcover.co.uk/test_new/css/header.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://www.swimmingcover.co.uk/test_new/css/font-awesome.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script src="http://www.swimmingcover.co.uk/test_new/js/mobile.js" type="text/javascript"></script>
</head>

<body>
<div id="header">
    <ul class="hdr_link_icons">
        <li class="icon_basket"><img src="http://www.swimmingcover.co.uk/test_new/images/header/icon_basket.jpg" alt="My basket" /><i class="fa fa-chevron-down"></i></li>
        <li class="icon_help"><img src="http://www.swimmingcover.co.uk/test_new/images/header/icon_help.jpg" alt="Help Section" /><i class="fa fa-chevron-down"></i></li>
    </ul>    
    <ul class="hdr_dropdown_area" id="help_dropdown">
        <li><a href="">Your Account <span class="hdr_link_arrow">&#187;</span></a></li>
        <li><a href="">Corporate Sales <span class="hdr_link_arrow">&#187;</span></a></li>
        <li><a href="">Got a voucher? <span class="hdr_link_arrow">&#187;</span></a></li>
    </ul>
    <div id="dvBasket" class="basketoverview hdr_dropdown_area">
        <div class="heading">Total items: 1</div>
        <div class="item">
            <div class="details">
                <a href="#">
                    Product 1
                </a> 
                <br />
                <span class="price">£39.00</span>
            </div>        
        </div>
        <div class="item">
            <div class="details">
                <a href="#">
                    Product 2
                </a> 
                <br />
                <span class="price">£39.00</span>
            </div>        
        </div>
        <div class="bottom">        
            <a title="Your basket" href="#">View Basket</a>
        </div>
    </div>
</div>

</body>
</html>

回答1:

I updated the fiddle, you could use this line for each one

$(this).children(".fa-chevron-down, .fa-chevron-up").toggleClass( 'fa-chevron-down fa-chevron-up'); 

http://jsfiddle.net/2srx4sv2/10/