简体   繁体   English

如何使jquery.tabs需要 <li> 名称而不是<a>href</a>

[英]how to make jquery.tabs takes <li> name instead of <a> href

here is an example of what i need to achieve when i click on a <li> i want to change the tabs instead of clicking on <a> i need it for a reasons is there is a way to achieve that 这是我单击<li>要更改的选项卡而不是单击<a>时需要实现的示例,由于某种原因,我需要它,但有一种方法可以实现

<script>
   $(function() {
     $( "#tabs" ).tabs();

   });
   </script>

    <div id="tabs">
        <ul>
            <li name='tabs-1'>tab1</li>
            <li name='tabs-2'>tab2</li>
            <li name='tabs-3'>tab3</li>
        </ul>
        <div id="tabs-1">...</div>
        <div id="tabs-2">...</div>
        <div id="tabs-3">...</div>
    </div>

 $(document).ready(function(){ $('#myTab li:last').tab('show') }); 
 .nav-tabs li { margin:10px; padding:10px; backgroung-color:#c2c2c2; border-left:1px solid #000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav nav-tabs" id="myTab"> <li class="active" data-target="#home" data-toggle="tab">Home</li> <li data-target="#profile" data-toggle="tab">Profile</li> <li data-target="#messages" data-toggle="tab">Messages</li> <li data-target="#settings" data-toggle="tab">Settings</li> </ul> <div class="tab-content"> <div class="tab-pane active" id="home">Home</div> <div class="tab-pane" id="profile">Profile</div> <div class="tab-pane" id="messages">Message</div> <div class="tab-pane" id="settings">Settings</div> </div> 

Hope above solution meet your requirement 希望以上解决方案满足您的要求

Since I understood you want to change tabs by clicking on <li> instead of clicking on <a> , maybe this will work: 由于我了解到您想通过单击<li>而不是单击<a>来更改标签,所以也许可以这样做:

$("li").on("click", function(){
    var tab_id = $(this).attr("name");
    $("#tabs div").each(function(){  //Hide all tabs
        $(this).hide();
    });
    $("#"+tab_id).show(); //Show tab
});

The "a" tag is an inline level element. “ a”标签是内联级别元素。 No inline level element may have its width set. 没有内联级别元素可以设置其宽度。 Why? 为什么? Because inline level elements are meant to represent flowing text which could in theory wrap from one line to the next. 因为内联级别的元素是要表示流畅的文本的,所以理论上可以从一行换行到另一行。 In those sorts of cases, it doesn't make sense to supply the width of the element, because you don't necessarily know if it's going to wrap or not. 在这种情况下,提供元素的宽度是没有意义的,因为您不一定知道它是否要包装。 In order to set its width, you must change its display property to block, or inline-block: 为了设置其宽度,必须将其显示属性更改为block或inline-block:

Just style the A with a display:block;: 只需使用display:block ;:给A设置样式即可:

#yourselector ul li a { display: block;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM