如何使用jQuery在垂直标签和垂直文本之间切换

时间:2018-06-23 06:41:43

标签: javascript jquery html5 twitter-bootstrap css3

此代码显示带有垂直文本和内容的垂直标签。切换无法正常工作,当我单击第二个选项卡时,也显示了第一个内容。谁能帮助我获取各个标签的内容?在这里,我提到了html,css和javascript代码。有可能在jQuery中吗?谁能提及细节?

<main id="main">       
<section id="portfolio"> 
<div class="container-fluid">
  <div class="row">
   <div class="col-lg-3 col-md-4">
    <div class="vtab">
      <div class="col-xs-3">
          <ul class="nav nav-tabs tabs-left vertical-text">
              <li class="active"><a href="#home-v" data-toggle="tab">Home</a> 
               </li>
              <li><a href="#profile-v" data-toggle="tab">Profile</a></li>
             </ul>
      </div>
      <div class="col-xs-9">
          <div class="tab-content">
        <div id="left" >
                    <div>
          <ul id="tree1" >
                    <li id="li2">News
                  <ul>
                      <li>Company Maintenance</li>
                      <li>Employees
                          <ul>
                              <li>Reports
                                  <ul>
                                      <li>Report1</li>
                                      <li>Report2</li>
                                      <li>Report3</li>
                                  </ul>
                              </li>
                              <li>Employee Maint.</li>
                          </ul>
                      </li>
                      <li>Human Resources</li>
                  </ul>
              </li>

               <li id="li2">Sites
                  <ul>
                      <li>Company Maintenance</li>
                      <li>Employees
                          <ul>
                              <li>Reports
                                  <ul>
                                      <li>Report1</li>
                                      <li>Report2</li>
                                      <li>Report3</li>
                                  </ul>
                              </li>
                              <li>Employee Maint.</li>
                          </ul>
                      </li>
                      <li>Human Resources</li>
                  </ul>
              </li>
          </ul>
        </div>
    </div>
              <div class="tab-pane" id="profile-v">Profile Tab.</div>
              </div>
      </div>
      <div class="clearfix"></div>    
        </div>    
        </div>
        </div>
        </div>

      </div>
    </section>

<script type="text/javascript">
  $.fn.extend({
    treed: function (o) {

      var openedClass = 'glyphicon-minus-sign';
      var closedClass = 'glyphicon-plus-sign';

      if (typeof o != 'undefined'){
        if (typeof o.openedClass != 'undefined'){
        openedClass = o.openedClass;
        }
        if (typeof o.closedClass != 'undefined'){
        closedClass = o.closedClass;
        }
      };
        var tree = $(this);
        tree.addClass("tree");
        tree.find('li').has("ul").each(function () {
            var branch = $(this); //li with children ul
            branch.prepend("<i class='indicator glyphicon " + closedClass + "'></i>");
            branch.addClass('branch');
            branch.on('click', function (e) {
                if (this == e.target) {
                    var icon = $(this).children('i:first');
                    icon.toggleClass(openedClass + " " + closedClass);
                    $(this).children().children().toggle();
                }
            })
            branch.children().children().toggle();
        });
      tree.find('.branch .indicator').each(function(){
        $(this).on('click', function () {
            $(this).closest('li').click();
        });
      });
        tree.find('.branch>a').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
        tree.find('.branch>button').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
    }
});

$('#tree1').treed();

  <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">  
  <link 
    href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" 
    rel="stylesheet" id="bootstrap-css">
 <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"> 
 </script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap- 
 combined.min.css" rel="stylesheet" id="bootstrap-css">
 <script src="//netdna.bootstrapcdn.com/twitter- 
        bootstrap/2.3.2/js/bootstrap.min.js"></script> 

1 个答案:

答案 0 :(得分:0)

您忘记了在选项卡面板上导入jQuery(JSfiddle控制台会引发该错误)以及Boostrap类(选项卡窗格)和id( a 标签的href内容)的导入。

这里是正确的代码:

来自CDN的jQuery脚本:

<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

以下是右侧面板代码:

<section id="portfolio">
   <div class="container-fluid">
      <div class="row">
         <div class="col-lg-3 col-md-4">
            <div class="vtab">
               <div class="col-xs-3">
                  <hr/>
                  <ul class="nav nav-tabs tabs-left vertical-text">
                     <li class="active"><a href="#home-v" data-toggle="tab">Home</a></li>
                     <li><a href="#profile-v" data-toggle="tab">Profile</a></li>
                  </ul>
               </div>
               <div class="col-xs-9">
                  <!-- Tab panes -->
                  <div class="tab-content">
                     <div class="tab-pane" id="home-v">Home Tab.</div>
                     <div class="tab-pane" id="profile-v">Profile Tab.</div>
                  </div>
               </div>
               <div class="clearfix"></div>
            </div>
         </div>
      </div>
   </div>
   </div>
</section>

我简化了“主页”面板中的代码,以免发布大量不相关的代码。

希望这会有所帮助!

使用完整的代码工作codepen link