使用jquery切换.active类

 黄乐瞳_319 发布于 2023-02-10 13:30

我正试图.active用jquery 切换这个类,这是我到目前为止所得到的:

HTML


CSS

.downloadButtons{
    display: block;
    width: 780px;
    height: 200px;
    margin-left: 40px;
    margin-top: 30px;
}

/* GLOBALS - Dropdowns*/
.downloadFontIcons{
    /* Size and position */
    position: relative; /* Enable absolute positionning for children and pseudo elements */
    width: 184px;
    height: 44px;
    margin: 0 auto;
    text-align: center;
    line-height: 44px;
    margin-right: 68px;
    font-size: .7em;
    color: #9ea7b3;
    background-color: #F9FAFC;
    cursor: pointer;
    outline: none;
    border: 1px solid #eaedf1;
}   
.downloadFontIcons .dropdown{
    /* Size & position */
    position: absolute;
    top: 110%;
    left: 0px;
    right: 0px;

    /* Styles */
    background: white;
    -webkit-transition: all 0.3s ease-out;
    -webkit-transition-delay: 0s;
    -moz-transition: all 0.3s ease-out;
    -moz-transition-delay: 0s;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    -o-transition-delay: 0s;
    transition: all 0.3s ease-out;
    transition-delay: 0s;
    list-style: none;

    /* Hiding */
    opacity: 0;
    pointer-events: none;
}

::selection {
    background: transparent; 
}

::-moz-selection {
    background: transparent; 
}

.download-button-wrapper {
    *zoom: 1;
    float: left;
}

.downloadFontIcons  .dropdown li a {
    display: block;
    height: 44px;
    text-decoration: none;
    color: #9ea7b3;
    -webkit-transition: all 0.3s ease-out;
    -webkit-transition-delay: 0s;
    -moz-transition: all 0.3s ease-out;
    -moz-transition-delay: 0s;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    -o-transition-delay: 0s;
    transition: all 0.3s ease-out;
    transition-delay: 0s;
}


/* Hover state */
.downloadFontIcons .dropdown li:hover a {
    color: #FFFFFF;
    background-color:#FF562A;
}

/* Active state */

.downloadFontIcons.active .dropdown {
    opacity: 1;
    pointer-events: auto;
}

/* No CSS3 support */

.no-opacity       .downloadFontIcons .dropdown,
.no-pointerevents .downloadFontIcons .dropdown {
    display: none;
    opacity: 1; /* If opacity support but no pointer-events support */
    pointer-events: auto; /* If pointer-events support but no pointer-events support */
}

.no-opacity       .downloadFontIcons.active .dropdown,
.no-pointerevents .downloadFontIcons.active .dropdown {
    display: block;
}

JS

       function DropDown(el) {
                this.dd = el;
                this.initEvents();
            }
            DropDown.prototype = {
                initEvents : function() {
                    var obj = this;

                    obj.dd.on('click', function(event){
                        $(this).toggleClass('active');
                        event.stopPropagation();
                    }); 
                }
            }

            $(function() {

                var dd = new DropDown( $('#dd') );

                $(document).click(function() {
                    // all dropdowns
                    $('.wrapper-dropdown-2').removeClass('active');
                });

            });



            $(function() {

                var de = new DropDown( $('#de') );

                $(document).click(function() {
                    // all dropdowns
                    $('.wrapper-dropdown-2').removeClass('active');
                });

            });

            $(function() {

                var df = new DropDown( $('#df') );

                $(document).click(function() {
                    // all dropdowns
                    $('.wrapper-dropdown-2').removeClass('active');
                });

            }); 

的jsfiddle

我需要切换按钮的活动状态,例如当我点击desktop按钮时出现下拉列表,然后当我点击phone按钮时,我希望desktop列表消失.我认为需要对代码的这一部分进行更改,就像使用此代码一样,我认为我只是针对dd元素:

function DropDown(el) {
  this.dd = el;
  this.initEvents();
}
DropDown.prototype = {
  initEvents : function() {
    var obj = this;

    obj.dd.on('click', function(event){
      $(this).toggleClass('active');
      event.stopPropagation();
    });
  }
}

我对javascript知之甚少,所以不知道该怎么做.需要你的帮助.

1 个回答
  • 在您的initEvents函数中,您可以查找并删除按钮上的所有活动类,不包括单击的按钮.这应该会给你你想要的功能.

    initEvents : function() {
      var obj = this;
    
      obj.dd.on('click', function(event){
        $('.active').not($(this)).removeClass('active');
        $(this).toggleClass('active');
        event.stopPropagation();
      });
    }
    

    http://jsfiddle.net/UhSqd/1/

    2023-02-10 13:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有