如何解决如何避免Bootstrap标签的URL哈希?
选项卡主体,我希望活动选项卡不出现在URL中
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#menu1">Menu 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#menu2">Menu 2</a>
</li>
</ul>
<div class="tab-content border mb-3">
<div id="home" class="container tab-pane active"><br>
<h3>HOME</h3>
<p>Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div id="menu1" class="container tab-pane fade"><br>
<h3>Menu 1</h3>
<p>Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div id="menu2" class="container tab-pane fade"><br>
<h3>Menu 2</h3>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,totam rem aperiam.</p>
</div>
</div>
<p class="act"><b>Active Tab</b>: <span></span></p>
<p class="prev"><b>PrevIoUs Tab</b>: <span></span></p>
</div>
<script>
$(document).ready(function(){
$(".nav-tabs a").click(function(){
$(this).tab('show');
});
$('.nav-tabs a').on('shown.bs.tab',function(event){
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // prevIoUs tab
$(".act span").text(x);
$(".prev span").text(y);
});
});
</script>
</body>
{{1}}
解决方法
您只需在点击功能后添加event.preventDefault();
$(document).ready(function(){
$(".nav-tabs a").click(function(){
event.preventDefault();
$(this).tab('show');
});
$('.nav-tabs a').on('shown.bs.tab',function(event){
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
$(".act span").text(x);
$(".prev span").text(y);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="container mt-3">
<h2>Dynamic Tabs with jQuery</h2>
<p>Click on the Tabs to display the active and previous tab.</p>
navigation tabs home tab is active by default
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#menu1">Menu 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#menu2">Menu 2</a>
</li>
</ul>
<div class="tab-content border mb-3">
<div id="home" class="container tab-pane active"><br>
<h3>HOME</h3>
<p>Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div id="menu1" class="container tab-pane fade"><br>
<h3>Menu 1</h3>
<p>Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div id="menu2" class="container tab-pane fade"><br>
<h3>Menu 2</h3>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,totam rem aperiam.</p>
</div>
</div>
<p class="act"><b>Active Tab</b>: <span></span></p>
<p class="prev"><b>Previous Tab</b>: <span></span></p>
</div>
javascript code to allow navigation between tabs
</body>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。