标签样式不适用

时间:2015-03-26 22:52:05

标签: jquery css3 tabs

如何在单击链接时动态地将负margin-bottom应用于元素。单击时,尝试删除选项卡的下边框。当我对标签元素应用负边距时,它可以工作,但它适用于所有看起来很有趣的标签。有人可以看看吗?

jQuery(document).ready(function() {
    jQuery('.tabs .tab-links a').on('click', function(e)  {
        var currentAttrValue = jQuery(this).attr('href');
 
        // Show/Hide Tabs
        jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
 
        // Change/remove current tab to active
        jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
       // tried adding this code but it doesn't seem to work
        $(function() {
    		$('tab-links a').click(function () {
       			$( ".tab-links.active" ).css('margin-bottom','-2px');
    		});
		});

 
        e.preventDefault();

    });
});
/*----- Tabs -----*/
.tabs {
    width:100%;
    display:inline-block;
}
 
    /*----- Tab Links -----*/
    /* Clearfix */
    .tab-links:after {
        display:block;
        clear:both;
        content:'';
        margin-bottom: -18px;
    }
 
    .tab-links li {
        margin:0px 5px;
        margin-bottom: 0px;
        float:left;
        list-style:none;
        border-radius:3px 3px 0px 0px;
        border: 2px solid black;
        margin-bottom: -2px;
        
        

    }
    .tab-links li.active {
        border-bottom: none;
        /*margin-bottom: -2px;  tried declaring a margin here but it didn't work */
      
    }
 
        .tab-links a {
            padding:10px 15px;
            display:inline-block;
            border-radius:3px 3px 0px 0px;
            background:#7FB5DA;
            font-size:16px;
            font-weight:600;
            color:#4c4c4c;
            transition:all linear 0.15s;
           
            
        }
 
        .tab-links a:hover {
            background:#a7cce5;
            text-decoration:none;
        }

    li.active a {
        background:#fff;
        color:#4c4c4c;
        border-bottom: none;     
    }
    li.active a {
        border-bottom: none;
    }
    /*----- Content of Tabs -----*/
    .tab-content {
        padding: 0 10px 10px 10px;
        border-radius:3px;
        box-shadow:-1px 1px 1px rgba(0,0,0,0.15);
        background:#fff;
        border: 2px solid black;
        

    }
 
        .tab {
            display:none;
        }
 
        .tab.active {
            display:block;
        }
<body>
    <div class="tabs">
        <ul class="tab-links">
            <li class="active"><a href="#tab1">Tab #1</a></li>
            <li><a href="#tab2">Tab #2</a></li>
            <li><a href="#tab3">Tab #3</a></li>
            <li><a href="#tab4">Tab #4</a></li>
        </ul>   
        <div class="tab-content">
            <div id="tab1" class="tab active">
                <p>Tab #1 content goes here!</p>
                <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis.</p>
            </div>
     
            <div id="tab2" class="tab">
                <p>Tab #2 content goes here!</p>
                <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
            </div>
     
            <div id="tab3" class="tab">
                <p>Tab #3 content goes here!</p>
                <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum ri.</p>
            </div>
     
            <div id="tab4" class="tab">
                <p>Tab #4 content goes here!</p>
                <p>Donec pulvinar neque sed semper lacinia. Curabitur lacinia ullamcorper nibh; quis imperdiet velit eleifend ac. Donec blandit mauris eget aliquet lacinia! Donec pulvinar massa interdum risus ornare mollis. In hac habitasse platea dictumst. Ut euismod tempus hendrerit. Morbi ut adipiscing nisi. Etiam rutrum sodales gravida! Aliquam tellus orci, iaculis vel.</p>
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
</body>

1 个答案:

答案 0 :(得分:0)

尝试使用toggleClass,如下所示:

JS:

$( ".tab-links a" ).click(function() {
  $( this ).toggleClass( "neq-margin" );
});

CSS:

.neq-margin  {
  margin-bottom: -2px;
}