代码之家  ›  专栏  ›  技术社区  ›  Jarad

单击引导程序4列表组面板父项时将URL片段附加到URL

  •  0
  • Jarad  · 技术社区  · 6 年前

    <a href="node##" ...></a> 引用HTML id list-group-item 节点?

    例如,如果我单击“园艺”列表组项,它将展开,显示其下的项,但我也希望同时将 #node11

    not expanded menu

    期望结果

    如果当前页是 http://www.example.com/home/ . 当我单击“园艺”菜单项时,菜单将展开,URL将更改为 http://www.example.com/home/#node11 http://www.example.com/home/#node13 . 因此,URL会在 列表组项 已单击具有子项的项。

    expanded

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css"
          integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
    
    <div class="container-fluid">
      <div class="row">
        <div class="text-nowrap">
          <div id="sidebar">
            <div class="list-group panel">
              <a href="#node11" class="list-group-item level-0" data-parent="#sidebar" data-toggle="collapse"
                 aria-expanded="true">Gardening <i class="fa fa-caret-down"></i></a>
              <div class="collapse show" id="node11" style="">
                <a href="#node13" class="list-group-item level-1" data-parent="#node13" data-toggle="collapse">Lawn
                  Chemicals <i class="fa fa-caret-down"></i></a>
                <div class="collapse" id="node13">
                  <a href="gardening/lawn-chemicals/moss-control/" class="list-group-item level-2" data-parent="#node13">
                    Moss Control</a>
                </div>
                <a href="gardening/lawn-mowing/" class="list-group-item level-1" data-parent="#node11">Lawn Mowing</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    

    为什么我对此感到困惑:

    这个 <a href="">

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jarad    6 年前

    我发现的一个解决方案是像这样使用javascript:

    <script>
        $('.list-group').on('click', '.list-group-item', function () {
            var clickedID = this.id;
            window.location.hash = clickedID;
            alert(clickedID);
        });
    </script>
    

    完整示例:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
            integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
            integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css"
          integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
    
    <div class="container-fluid">
      <div class="row">
        <div class="text-nowrap">
          <div id="sidebar">
            <div class="list-group panel">
              <a href="#node11" class="list-group-item level-0" data-parent="#sidebar" data-toggle="collapse"
                 aria-expanded="true" id="gardening">Gardening <i class="fa fa-caret-down"></i></a>
              <div class="collapse" id="node11" style="">
                <a href="#node13" class="list-group-item level-1" data-parent="#node13" data-toggle="collapse"
                id="lawn-chemicals">Lawn Chemicals <i class="fa fa-caret-down"></i></a>
                <div class="collapse" id="node13">
                  <a href="gardening/lawn-chemicals/moss-control/" class="list-group-item level-2" data-parent="#node13"
                  id="moss-control">Moss Control</a>
                </div>
                <a href="gardening/lawn-mowing/" class="list-group-item level-1" data-parent="#node11"
                id="lawn-mowing">Lawn Mowing</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <script>
        $('.list-group').on('click', '.list-group-item', function () {
            var clickedID = this.id;
            window.location.hash = clickedID;
            alert(clickedID);
        });
    </script>
    

    带走 :当其中一个链接 list-group-item 已单击。在这种情况下,我必须添加一个独特的 id 列表组项

    下一步

    if(window.location.hash) {
        var hash = window.location.hash.slice(1);
        var elem = document.getElementById(hash);
        var node = elem.hash.slice(1);
        var x = document.getElementById(node);
        x.classList.add('show');
    } else {
      // Fragment doesn't exist
    }