重写活动类(jQuery)后突出显示活动选项卡

时间:2016-08-17 22:49:09

标签: javascript jquery html css twitter-bootstrap

我遇到突出显示导航栏(bootstrap)的活动标签的问题

页面的工作原理如下:

  1. 用户添加产品
  2. 添加后,将显示一个新标签,其中显示的产品名称为
  3. 然后,用户可以单击该选项卡以检查属于该产品的复选框
  4. 继续添加产品和/或删除产品
  5. 在第3步中,标签颜色会更新,以匹配标签中进度条的颜色。

    我的问题是我希望选项卡在选中(活动)时具有不同的颜色,然后在单击另一个选项卡后恢复为进度条颜色。

    代码:

    $(document).ready(function () {
        $('#new').keypress(function (e) {
            var key = e.which;
            if (key == 13) {
                AddTabFromTemplate($('#newProductName').val());
            }
        })
    
        $('#btnAddNew').on('click', function () {
            AddTabFromTemplate($('#newProductName').val());
        });
    
        // Listen for all js-deletetab in the context of #tab-content, even if the tab panels are dynamically generated
        $('#tab-content').on('click', '.js-deletetab', function () {
            // Find the parent tab-pane's ID and call the Remove Tab function
            RemoveTab($(this).parents('.tab-pane').attr('id'));
        });
    });
    
    function AddTabFromTemplate(newID) {
        // Sanitize ID in here is fine
        var domID = newID.replace(/ /g, '_');
    
        // Make a copy of the template
        $newTab = $('#template').clone();
        $newTab.attr('id', domID);
    
        // Insert the new tab page right before our "new" tab.
        $newTab.insertBefore('#new');
    
        // Create the tab item, too
        var tab = $('<li role="presentation"><a href="#' + domID + '" role="tab" data-toggle="tab">' + newID + '</a></li>').insertBefore('#newTab');
        tab = tab.find('a');
    
        //Update ProgressBar
        ProgressBar($newTab, tab);
    
    }
    
    //Uses the newly created tab to target its progress bar
    function ProgressBar( currentTab, liTab){
        //Grab all the checkboxes
        var checkboxes = currentTab.find('.checkbox');
        liTab.css({ 
            'background-color': '#D9534F',// red = #D9534F;
            'color': 'white'
        });
        //Grab the Landing required checkboxes
        var landDisp = currentTab.find('.land-display');
        var landingPage = currentTab.find('.req');
        var landReq = false;
        landDisp.css('display', 'none');
    
        //If landing page is required display the rest of the checkboxes
        //  and updated the landReq bool
        landingPage.on('click', function(){
            if(landReq === false){
                landDisp.toggle();
                landReq = true;
            }
            else if(landReq === true){
                landDisp.toggle();
                landReq = false;
            }
        })
    
        checkboxes.on('click', function(){
            var emptyValue = 0;
            //Checks each checkbox in the tab for checked or not checked
            checkboxes.each(function() {                
                if($(this).is(':checked')){
                    //If the landing required checkbox is checked update 
                    //  the value of the emptyValue for progress bar
                    if(landReq === false){
                        emptyValue += 5.3;
                    }
                    else if(landReq === true){
                        emptyValue += 4.4;
                    }
                }
            });
    
            //Progressbar update section
            if(emptyValue > 30 && emptyValue < 70 ){
                currentTab.find('.progress-bar').removeClass('progress-bar-danger');
                currentTab.find('.progress-bar').addClass('progress-bar-warning');
                liTab.css('background-color', '#F0AD4E');// yellow = #F0AD4E;
            }
            else if(emptyValue >= 70){
                currentTab.find('.progress-bar').removeClass('progress-bar-warning');
                currentTab.find('.progress-bar').addClass('progress-bar-success');
                liTab.css('background-color', '#5CB85C');// green = #5CB85C;
            }
            currentTab.find('.progress-bar').css('width', emptyValue + '%').attr('aria-valuenow', emptyValue);
        });
    }
    

    Codepen

    感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以添加

li.active a {
    background-color: black !important;
}

这是working codepen

只要a具有li类,active标记的背景颜色就会为黑色。使用!important我们覆盖了a标记的内联样式定义。

相关问题