dongzanghong4379 2016-11-19 14:57
浏览 218
已采纳

循环遍历li元素,检查类并设置css

Hi there I have a problem with a jquery function. I have a account menu on my wordpress site. I want to set the opacity to 1 of the element who has the class is-active (the visible menu entry). When I click on a specific element (bsp: My Orders) it gets the class name is-active is active but the problem is that the opacity dont change. When I click on another element (bsp: My Account) the last one (My Orders) which I clicked change the opacity (but to late). This is so crazy and I dont have any idea.

My HTML Code:

<ul>
    <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--dashboard is-active active">
        <a href="https://mywebsite.com/en/my-account/" style="opacity: 0.5;" data-loaded="true">My Account</a> <!--This should be opacity: 1 because of is-active-->
    </li>
    <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--subscriptions">
        <a href="https://mywebsite.com/en/my-account/subscriptions/" style="opacity: 0.5;">Subscriptions</a>
    </li>
    <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--orders active"> 
        <a href="https://mywebsite.com/en/my-account/orders/" style="opacity: 1;" data-loaded="true">My Orders</a> <!--This should be opacity: .5-->
    </li>
</ul>

My jQuery code:

$(document).ready(function() { 
    $('.woocommerce-MyAccount-navigation-link.woocommerce-MyAccount-navigation-link').each(function(i, obj) {
        if ($(this).hasClass('is-active')) {
            var myaccountlink = $(this).attr('class');
            myaccountlink = myaccountlink.replace(/\s+/g, '.');
            $('.' + myaccountlink).find('a').css('opacity', '1', '!important');
        } else {
            $(this).find('a').css('opacity', '.5');
        }
    });
});

</div>
  • 写回答

1条回答 默认 最新

  • duanrebo3559 2016-11-19 15:16
    关注

    you can remove inline rules for opacity and apply css based on classes

    li{
      opacity:0.5;
      }
    li.active{
      opacity:1;
      }
    <ul>
            <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--dashboard is-active active">
                <a href="https://mywebsite.com/en/my-account/"  data-loaded="true">My Account</a> <!--This should be opacity: 1 because of is-active-->
            </li>
            <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--subscriptions">
                <a href="https://mywebsite.com/en/my-account/subscriptions/" >Subscriptions</a>
            </li>
            <li class="woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--orders active"> 
                <a href="https://mywebsite.com/en/my-account/orders/"  data-loaded="true">My Orders</a> <!--This should be opacity: .5-->
            </li>
        </ul>

    $(document).ready(function() { 
        $('.woocommerce-MyAccount-navigation-link.woocommerce-MyAccount-navigation-link').each(function(i, obj) {
            if ($(this).hasClass('is-active')) {
                var myaccountlink = $(this).attr('class');
                myaccountlink = myaccountlink.replace(/\s+/g, '.');
                $('.' + myaccountlink).find('a').css('opacity', '1', '!important');
            } else {
                $(this).find('a').css('opacity', '.5');
            }
        });
    });

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog