尝试使用jQuery设置onClick事件

时间:2016-03-27 06:20:40

标签: javascript jquery css

我有一个名为.dropdown-sub的类,另一个名为.dropdown-toggle。我想在点击.dropdown-toggle时单击.dropdown-sub向下滑动,再次单击时消失。

这是css。

/* Mega Menu */

.site-nav > li > a {
    padding: 20px;
  font-weight: bold;
}
.site-nav > li.mega-menu { position: static; }
.dropdown-sub { 
    display: none;
    position: absolute;
    top: 60px;
    left: 0;
    z-index: 5;
  padding: 35px 30px;
    margin-bottom: 0; 
  width: 100%;
  min-width: 215px;
} 
.wrapper-container .site-nav li.menu-dropdown:focus .dropdown-sub { display: block; text-align: left;}
.wrapper-container .site-nav li.dropdown:focus.mega-menu > a > .icon-dropdown { border-top-color: #fff; opacity: 1; }
.wrapper-container .site-nav li.mega-menu .nav-dropdown { width: auto; left: 0%; right: 0%; border: 0; padding: 40px 45px; }
.wrapper-container .site-nav li.mega-menu .nav-dropdown img{margin-top:37px;}

.nav-dropdown .menu-group ul.menu-dropdown { display: block; position: static; padding: 0; }

.site-nav li.mega-menu > .menu-dropdown:focus > .nav-dropdown, 
.site-nav li.mega-menu > .menu-dropdown.open > .nav-dropdown { display: block; }

.cbp-spmenu .nav-dropdown .col-1 { padding:0; }
.cbp-spmenu .nav-dropdown .col-1 .inner { position: relative; }
.cbp-spmenu .nav-dropdown .col-1 .inner > a { margin: 0; padding: 11px 0 13px; font-size: 11px; letter-spacing: 0.02em; border-top: 1px solid #e5e5e5; }
.cbp-spmenu .nav-dropdown .col-1 ul.menu-dropdown { display: none; padding: 0; margin-bottom: 27px; }
.cbp-spmenu .site-nav li.mega-menu li li a { font-size: 12px; padding: 9px 35px; }
.cbp-spmenu .site-nav li.mega-menu li li a:focus span { border-bottom: 1px solid #fff; }
.cbp-spmenu .site-nav li.mega-menu .nav-dropdown li:focus > a:focus, 
.cbp-spmenu .site-nav li.mega-menu .nav-dropdown li:focus > a, 
.cbp-spmenu .site-nav li.mega-menu .nav-dropdown a:focus { background: none; } 
.cbp-spmenu .site-nav li.mega-menu .nav-dropdown > p.toogleClick { top: 0; }

.sub-mega-menu { display: none; }

.addthis_native_toolbox .at-share-tbx-element { display: none; }
.addthis_native_toolbox .at-share-tbx-element:first-child { display: block; }

.site-nav .menu-dropdown .dropdown-sub .menu-title{
    font-size: 12px;
    font-weight: bold;
    letter-spacing: 1px;
    margin-bottom: 20px;
  text-transform: uppercase;}

.site-nav .menu-dropdown .dropdown-sub  ul.nav-links li a{
       font-weight: bold;
    padding: 7px 0;
}
.site-nav > li.focus > a:before, .site-nav > li:focus > a:before{
    content: '';
    position: absolute;
    height: 45px;
    width: 2px;
    background-color: #fff;
    -ms-transform: translate(0%, -50%) rotate(45deg);
    -webkit-transform: translate(0%, -50%) rotate(45deg);
    left: 50%;
    vertical-align: middle;
    display: table-cell;
    transform: translate(0%, -50%) rotate(45deg);
    top: 50%;
}
.site-nav > li.mega-menu.full-width .dropdown-sub{
    top: 140px;
    width: auto;
    left: 0%;
    right: 0%;
    border: 0;
    text-align: left;
    padding: 30px 60px;
}
.container{
    position: relative;
}



.site-nav > li {float: none; position: relative; display: inline-block; list-style: none; }
.site-nav > li > a {position: relative; text-transform: uppercase; padding: 22px 20px; letter-spacing: 1px; }
.site-nav > li:first-child > a { margin-left: 0; } 
.nav-dropdown {background:#fff; text-align: left; display: none; position: absolute; top: 62px; left: 0; width: 215px; z-index: 999; padding:  15px 20px; margin-bottom: 0; border: 1px solid transparent; }
.nav-dropdown li a { padding: 8px 0px 8px 0px; position: relative; }

.site-nav li:focus .dropdown-sub{opacity: 1;top: 58px;display:block;z-index: 99; visibility: visible;}

.site-nav .menu-dropdown li a{font-size:13px;letter-spacing: 0.5px; font-family: Lato;font-weight:400;text-transform: capitalize;}
.dropdown-sub {
    background: #fff none repeat scroll 0 0;
    left: 0;transition: all 0.3s ease-in-out 0s;
    opacity: 0;
    position: absolute;
    text-align: left;
    top: 100px; visibility: hidden;
    width: 100%;
    min-width: 220px;padding:0 15px;
    z-index: -1;
}

.dropdown-menu-inner {

    padding: 15px;
}
.menu-title {
    color: #000;
    font-family: Montserrat;
    font-size: 16px;margin-bottom:10px;
    font-weight: 600;
    text-transform: uppercase;
}
.lable-new::before {
    border-color: #333 transparent #fff;
    border-style: solid;
    border-width: 6px 4px 0 0;
    bottom: -6px;
    content: "";
    left: 6px;
    position: absolute;
}
.is-ticky .lable-new{top:-1px}
.lable-new {
    background: #333 none repeat scroll 0 0;
    border-radius: 1px;
    color: #fff;
    font-size: 10px;
    font-weight: 300;
    left: 50%;
    padding: 0 5px;
    position: absolute;
    top: -8px;
    z-index: 9;
}

.lable-hot::before {
    border-color: #cc0000 transparent #fff;
    border-style: solid;
    border-width: 6px 4px 0 0;
    bottom: -6px;
    content: "";
    left: 6px;
    position: absolute;
}
.is-ticky .lable-hot{top:-1px}
.lable-hot {
    background: #cc0000 none repeat scroll 0 0;
    border-radius: 1px;
    color: #fff;
    font-size: 10px;
    font-weight: 300;
    left: 50%;
    padding: 0 5px;
    position: absolute;
    top: -8px;
    z-index: 9;
}
#product-menu .sca-qv-button-wrap{display:none!important}
#product-menu .grid-item{padding:0 7px;}

这是js:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // build a variable to target the #menu div
    var menu = $('.dropdown-sub')
    // bind a click function to the menu-trigger
    $('.dropdown-toggle').click(function(event){
        event.preventDefault();
        // if the menu is visible slide it up
        if (menu.is(":visible"))
        {
            menu.slideUp(400);
            document.getElementsByClassName('dropdown-sub').style.display = 'none';
        }
        // otherwise, slide the menu down
        else
        {
            menu.slideDown(400);
            document.getElementsByClassName('dropdown-sub').style.display = 'block';
        }
    });
});
</script>

我在Google Chrome控制台上收到此错误&#34; 未捕获TypeError:无法设置属性&#39;显示&#39;未定义的&#34;

2 个答案:

答案 0 :(得分:0)

if (menu.is(":visible"))
        {
            menu.slideUp(400);
            $('.dropdown-sub').css('visibility', 'hidden');
        }
        // otherwise, slide the menu down
        else
        {
            menu.slideDown(400);
            $('.dropdown-sub').css('visibility', 'visible');
        }

OR

if(menu.is(":visible")) {
    menu.slideUp(400);
    $(dropdown-sub).toggleClass('visible invisible');
} else {
    menu.slideDown(400);
    $(dropdown-sub).toggleClass('visible invisible');
}}

Make sure .dropdown-sub starts off with this: .visible { visibility: visible;} or .invisible { visibility: hidden; } 

答案 1 :(得分:0)

要对代码进行一些改变。

1)将if else中的代码更改为以下内容。

    if (menu.is(":visible"))
    {
        menu.slideUp(400);
        document.getElementsByClassName('dropdown-sub')[0].style.display = 'none';//added [0]
        //$('.dropdown-sub').css('display','none');
    }
    // otherwise, slide the menu down
    else
    {
        menu.slideDown(400);
        document.getElementsByClassName('dropdown-sub')[0].style.display = 'block'; //added[0]
        //$('.dropdown-sub').css('display','block');
    }

解释。

  

getElementsByClassName()方法返回一个的集合   具有指定类名的元素的子元素,作为NodeList   对象

     

NodeList对象表示节点的集合。节点可以   通过索引号访问。索引从0开始。

<强> 2)

我认为您忘记制作元素display:block,然后开始向下滑动。否则menu.slideDown(400);将向下滑动隐藏元素,因此您可能看不到动画。所以在你的其他部分这样做。

else
    {          
        document.getElementsByClassName('dropdown-sub')[0].style.display = 'block'; //added[0]
        //$('.dropdown-sub').css('display','block');
        menu.slideDown(400);
    }

注意:由于您已经在使用jquery,因此不必使用document.getElementsByClassName这是一种javascript语法。使用$('.dropdown-sub') jquery语法。我已将它们放在代码中,但已评论过。