doubi7739 2015-12-08 21:39
浏览 86
已采纳

jQuery slideToggle没有触发

So I am trying to create a dropdown menu with jquery and have a div (the dropdown) with display: none. I am trying to fire the jquery toggle when an li item in the above nav bar is clicked. If anyone could take a look at this code and see if there are any obvious errors, that would be great.

Thanks

PS The php code is present as I am working with WP

<ul class="right-nav">
    <li style="padding-left: 10px; cursor: pointer;" id="current-user">
    <?php $current_user = wp_get_current_user(); echo $current_user->user_login; ?>
    </li>
</ul>

The dropdown menu:

<div class="dropdown-wrapper">
    <div id="user-menu" class="user-menu">
       <ul>
           <li>Hello</li>
        </ul>
    </div>

.user-menu {
    display: none;
}

And then the jQuery event itself:

$(document).ready(function() {
        $('#current-user').click(function() {
                $('#user-menu').slideToggle("fast");
        });
    });
  • 写回答

1条回答 默认 最新

  • dongyi1159 2015-12-08 21:50
    关注

    Man... I guess I kinda got it. Remove the CSS:

    .user-menu {
        display: none;
    }
    

    And instead, in teh document's ready, give this:

    $(".user-menu").hide();
    

    Trust me, this makes a difference. The reason is, the jQuery first checks if the style="display: none;" is there or not in the element, as it is not there, because it is given by the CSS, it gives another display: none inline style. This doesn't make any difference. But third time click will reveal it. Give it a try.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥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,如何解決?