当引导程序4 <a href="node##" ...></a>
引用HTML id
来扩展垂直手风琴样式的扩展/折叠侧边栏时,如何强制地址栏附加{的URL片段{1}}个节点?
例如,如果单击“园艺”列表组项目,它会展开以显示其下的项目,但我也想同时将list-group-item
放入当前URL。
所需结果
如果当前页面是http://www.example.com/home/。当我单击“园艺”菜单项时,菜单将展开并且URL更改为http://www.example.com/home/#node11。然后,如果我单击“草坪化学制品”菜单项,则URL更改为http://www.example.com/home/#node13。因此,当单击具有子项的#node11
项时,URL将添加和/或更新节点ID。
可复制的示例:
list-group-item
为什么我对此感到困惑:
<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>
引用HTML ID或实际URL。当他们引用HTML ID时,我不确定如何使其也更改当前URL。
答案 0 :(得分:0)
我发现一个解决方案是像这样使用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
之一时,javascript将URL片段(哈希)添加到URL。在这种情况下,我必须为每个id
添加一个唯一的list-group-item
,所以我使用了类似块的值。
下一步是要弄清楚如何打开手风琴导航到用户最后一次单击的元素,例如,如果他们在浏览器中回击(这样用户就不必重新打开树)。不确定如何执行此操作,但这是我的位置:
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
}