我发现的一个解决方案是像这样使用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
}