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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度