获取Bootstrap 3选项卡窗格以使用TreeView

时间:2015-02-23 13:44:41

标签: twitter-bootstrap-3 tabs treeview

我正在尝试从BootSnipp获取TreeView:1以使用Bootstrap 3标签。

它最初按预期工作,但点击几下就会崩溃,然后响应/不响应任意。这是我正在使用的html + js + css:

// ***************** BEGIN TREE JS ***************** 
$.fn.extend({
  treed: function() {
    return this.each(function() {
      //initialize each of the top levels
      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 glyphicon-plus-sign'></i>");
        branch.addClass('branch');
        branch.on('click', function(e) {
          if (this == e.target) {
            var icon = $(this).children('i:first');
            icon.toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
            $(this).children().children().toggle();
          }
        })
        branch.children().children().toggle();
      });
      //fire event from the dynamically added icon
      $('.branch .indicator').on('click', function() {
        $(this).closest('li').click();
      });
      //fire event to open branch if the li contains an anchor instead of text
      $('.branch a').on('click', function(e) {
        $(this).closest('li').click();
        e.preventDefault();
      });
      //fire event to open branch if the li contains a button instead of text
      $('.branch button').on('click', function(e) {
        $(this).closest('li').click();
        e.preventDefault();
      });
    });
  }
});


$('.tree').treed();
//*****************  END TREE JS *****************
/*BEGIN TREE CSS*/

.tree,
.tree ul {
  margin: 0;
  padding: 0;
  list-style: none
}
.tree ul {
  margin-left: 1em;
  position: relative
}
.tree ul ul {
  margin-left: .5em
}
.tree ul:before {
  content: "";
  display: block;
  width: 0;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  border-left: 1px solid
}
.tree li {
  margin: 0;
  padding: 0 1em;
  line-height: 2em;
  color: #369;
  font-weight: 700;
  position: relative
}
.tree ul li:before {
  content: "";
  display: block;
  width: 10px;
  height: 0;
  border-top: 1px solid;
  margin-top: -1px;
  position: absolute;
  top: 1em;
  left: 0
}
.tree ul li:last-child:before {
  background: #fff;
  height: auto;
  top: 1em;
  bottom: 0
}
.indicator {
  margin-right: 5px;
}
.tree li a {
  text-decoration: none;
  color: #369;
}
.tree li button,
.tree li button:active,
.tree li button:focus {
  text-decoration: none;
  color: #369;
  border: none;
  background: transparent;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
  outline: 0;
}
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-7">
      <ul id="tabs" class="tree">
        <li><a href="#panel-continent-overview" data-target="#panel-continent-overview" role="tab" data-toggle="tab">Continent Overview</a>
        </li>

        <li><a href="#">Countries</a>
          <ul>

            <li><a href="#">Country 1</a>
              <ul>
                <li><a href="#panel-country-9400470e-fa49-46ea-b587-2ceb108600c0" data-target="#panel-country-9400470e-fa49-46ea-b587-2ceb108600c0" role="tab" data-toggle="tab">Country Overview</a>
                </li>




              </ul>
            </li>

            <li><a href="#">Country 2</a>
              <ul>
                <li><a href="#panel-country-45f97844-9773-4059-bafd-a2782fc06db1" data-target="#panel-country-45f97844-9773-4059-bafd-a2782fc06db1" role="tab" data-toggle="tab">Country Overview</a>
                </li>


                <li><a href="#">Cities</a>
                  <ul>

                    <li><a href="#panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a" data-target="#panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a" role="tab" data-toggle="tab">City P</a>
                    </li>


                  </ul>
                </li>



              </ul>
            </li>

          </ul>
        </li>

        <li><a href="#">Cities</a>
          <ul>

            <li>
              <a href="#">City A</a>
              <ul>
                <li><a href="#panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18" data-target="#panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18" role="tab" data-toggle="tab">Overview</a>

                </li>
              </ul>
            </li>

            <li>
              <a href="#">City B</a>
              <ul>
                <li><a href="#panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd" data-target="#panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd" role="tab" data-toggle="tab">Overview</a>

                  <li><a href="#">Neighborhoods</a>
                    <ul>

                      <li><a href="#panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71" data-target="#panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71" role="tab" data-toggle="tab">Neighborhood X</a>
                      </li>

                    </ul>
                  </li>

                </li>
              </ul>
            </li>

          </ul>
        </li>

        <li><a href="#">Neighborhoods</a>
          <ul>

            <li><a href="#panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19" data-target="#panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19" role="tab" data-toggle="tab">Neighborhood T</a>
            </li>

          </ul>
        </li>
      </ul>
    </div>

    <div class="col-xs-5">
      <div class="tabbable" id="tabbable-proj">

        <div class="tab-content main-tab-content">
          <div class="tab-pane fade in active" id="panel-continent-overview">
            <strong>Continent Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-country-9400470e-fa49-46ea-b587-2ceb108600c0">
            <strong>Country Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-country-45f97844-9773-4059-bafd-a2782fc06db1">
            <strong>Country Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a">
            <strong>City Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-neighborhood-babea547-cfb6-4e12-8c4a-bbbb25068d8a">
            <strong>Neighborhood Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18">
            <strong>City Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd">
            <strong>City Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71">
            <strong>Neighborhood Details</strong>
          </div>

          <div class="tab-pane fade" id="panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19">
            <strong>Neighborhood Details</strong>
          </div>

        </div>
      </div>
    </div>
  </div>
</div>

我完全不知道是什么导致了这个问题。有人可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

好的,我会自己尝试一下。

它没有用,因为Bootstrap期望标签是一个扁平的ul - 我有多个嵌套的uls。

我的工作是

  1. 将树与可能的树分开;
  2. 为标签创建隐藏的平面ul;和
  3. 挂钩相关的树锚点击,在相应的标签上手动拨打$(&#39; ...)。标签(&#39;显示&#39;)。
  4. 我还发现使用此策略,我可以从页面的任何位置调出我的标签 - 例如来自面包屑或随机锚。

    现在我知道我的问题对于那里的大多数人来说太苛刻了 - html本身就到处都是。

    我能够解决的答案看起来有点hackish。至少有人会评论它有多好或多坏?比如,它会因为隐藏ul而导致搜索引擎处罚吗?对可访问性的影响是什么 - 特别是当我从锚标记中删除文本时?

    或者我应该发布一个或多个新问题来征求此类反馈?

    干杯

    &#13;
    &#13;
    // ***************** BEGIN TREE JS ***************** 
    $.fn.extend({
      treed: function() {
        return this.each(function() {
          //initialize each of the top levels
          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 glyphicon-plus-sign'></i>");
            branch.addClass('branch');
            branch.on('click', function(e) {
              if (this == e.target) {
                var icon = $(this).children('i:first');
                icon.toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
                $(this).children().children().toggle();
              }
            })
            branch.children().children().toggle();
          });
          //fire event from the dynamically added icon
          $('.branch .indicator').on('click', function() {
            $(this).closest('li').click();
          });
          //fire event to open branch if the li contains an anchor instead of text
          $('.branch a').on('click', function(e) {
            $(this).closest('li').click();
            e.preventDefault();
          });
          //fire event to open branch if the li contains a button instead of text
          $('.branch button').on('click', function(e) {
            $(this).closest('li').click();
            e.preventDefault();
          });
        });
      }
    });
    
    
    $('.tree').treed();
    //*****************  END TREE JS *****************
    $(document).ready(function() {
      $("a.tree-link").on('click', function (event) {
        var hrefTab = $(this).attr('href');
        $('#hidden-tabs a[href='+hrefTab+']').tab('show');
      });
    });
    &#13;
    #hidden-tabs{
      height: 0px;
      list-style-type: none;
      padding:0; 
      margin:0;
    }
    
    #hidden-tabs{
      height: 0px;
      list-style-type: none;
      padding:0; 
      margin:0;
    }
    #hidden-tabs li{
      height: 0px;
      padding:0; 
      margin:0;
    }
    
    #hidden-tabs li a{
      height: 0px;
      padding:0; 
      margin:0;
    }
    
    
    /*BEGIN TREE CSS*/
    
    .tree,
    .tree ul {
      margin: 0;
      padding: 0;
      list-style: none
    }
    .tree ul {
      margin-left: 1em;
      position: relative
    }
    .tree ul ul {
      margin-left: .5em
    }
    .tree ul:before {
      content: "";
      display: block;
      width: 0;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      border-left: 1px solid
    }
    .tree li {
      margin: 0;
      padding: 0 1em;
      line-height: 2em;
      color: #369;
      font-weight: 700;
      position: relative
    }
    .tree ul li:before {
      content: "";
      display: block;
      width: 10px;
      height: 0;
      border-top: 1px solid;
      margin-top: -1px;
      position: absolute;
      top: 1em;
      left: 0
    }
    .tree ul li:last-child:before {
      background: #fff;
      height: auto;
      top: 1em;
      bottom: 0
    }
    .indicator {
      margin-right: 5px;
    }
    .tree li a {
      text-decoration: none;
      color: #369;
    }
    .tree li button,
    .tree li button:active,
    .tree li button:focus {
      text-decoration: none;
      color: #369;
      border: none;
      background: transparent;
      margin: 0px 0px 0px 0px;
      padding: 0px 0px 0px 0px;
      outline: 0;
    }
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
    
        <div class="container">
          <div class="row">
            <div class="col-xs-7">
              <ul class="tree">
                <li><a href="#panel-continent-overview"  class="tree-link">Continent Overview</a>
                </li>
    
                <li><a href="#">Countries</a>
                  <ul>
    
                    <li><a href="#">Country 1</a>
                      <ul>
                        <li><a href="#panel-country-9400470e-fa49-46ea-b587-2ceb108600c0"  class="tree-link">Country Overview</a>
                        </li>
    
    
    
    
                      </ul>
                    </li>
    
                    <li><a href="#">Country 2</a>
                      <ul>
                        <li><a href="#panel-country-45f97844-9773-4059-bafd-a2782fc06db1"  class="tree-link">Country Overview</a>
                        </li>
    
    
                        <li><a href="#">Cities</a>
                          <ul>
    
                            <li><a href="#panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a"  class="tree-link">City P</a>
                            </li>
    
    
                          </ul>
                        </li>
    
    
    
                      </ul>
                    </li>
    
                  </ul>
                </li>
    
                <li><a href="#">Cities</a>
                  <ul>
    
                    <li>
                      <a href="#">City A</a>
                      <ul>
                        <li><a href="#panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18"  class="tree-link">Overview</a>
    
                        </li>
                      </ul>
                    </li>
    
                    <li>
                      <a href="#">City B</a>
                      <ul>
                        <li><a href="#panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd"  class="tree-link">Overview</a>
    
                          <li><a href="#">Neighborhoods</a>
                            <ul>
    
                              <li><a href="#panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71"  class="tree-link">Neighborhood X</a>
                              </li>
    
                            </ul>
                          </li>
    
                        </li>
                      </ul>
                    </li>
    
                  </ul>
                </li>
    
                <li><a href="#">Neighborhoods</a>
                  <ul>
    
                    <li><a href="#panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19"  class="tree-link">Neighborhood T</a>
                    </li>
    
                  </ul>
                </li>
              </ul>
            </div>
    
            <div class="col-xs-5">
              <div class="tabbable" id="tabbable-proj">
    
                <ul id="hidden-tabs"  role="tablist" aria-expanded="false">
                  <li>
                    <a href="#panel-continent-overview" aria-controls="panel-continent-overview" role="tab" data-toggle="tab"></a>
                  </li>
                  
                  <li>
                    <a href="#panel-country-9400470e-fa49-46ea-b587-2ceb108600c0" aria-controls="panel-country-9400470e-fa49-46ea-b587-2ceb108600c0" role="tab" data-toggle="tab"></a>
                  </li>
    
                  <li>
                    <a href="#panel-country-45f97844-9773-4059-bafd-a2782fc06db1" aria-controls="panel-country-45f97844-9773-4059-bafd-a2782fc06db1" role="tab" data-toggle="tab"></a>
                  </li>
    
                  <li>
                    <a href="#panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a" aria-controls="panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a" role="tab" data-toggle="tab"></a>
                  </li>
    
                  <li>
                    <a href="#panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18" aria-controls="panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18" role="tab" data-toggle="tab"></a>
                  </li>
    
                  <li>
                    <a href="#panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd" aria-controls="panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd" role="tab" data-toggle="tab"></a>
                  </li>
    
                  <li>
                    <a href="#panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71" aria-controls="panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71" role="tab" data-toggle="tab"></a>
                  </li>
    
    
                  <li>
                    <a href="#panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19" aria-controls="panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19" role="tab" data-toggle="tab"></a>
                  </li>
                </ul>
    
                <div class="tab-content main-tab-content">
                  <div class="tab-pane fade in active" id="panel-continent-overview">
                    <strong>Continent Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-country-9400470e-fa49-46ea-b587-2ceb108600c0">
                    <strong>Country 1 Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-country-45f97844-9773-4059-bafd-a2782fc06db1">
                    <strong>Country 2 Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-city-2120d7aa-2b93-4d0d-a42b-8c6fedc9333a">
                    <strong>City P Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-neighborhood-babea547-cfb6-4e12-8c4a-bbbb25068d8a">
                    <strong>Neighborhood Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-city-b4a991ce-80ef-4406-b1d2-9892c0d93d18">
                    <strong>City A Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-city-2e6f1227-66d0-4061-9243-bf6cf4feaabd">
                    <strong>City B Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-neighborhood-f3fd5e2f-583d-4ccb-b163-03e118896f71">
                    <strong>Neighborhood X Details</strong>
                  </div>
    
                  <div class="tab-pane fade" id="panel-neighborhood-319d56ac-286f-4afa-af42-5ec74bbcac19">
                    <strong>Neighborhood T Details</strong>
                  </div>
    
                </div>
              </div>
            </div>
          </div>
        </div>
    &#13;
    &#13;
    &#13;