我有一些带有一些jquery的Bootstrap选项卡,可以在页面重新加载或超链接上显示特定的选项卡。 jQuery从1.11.3更新到2.2.4后,它停止工作并发出此错误:未捕获错误:语法错误,无法识别的表达式:.nav-tabs a
任何想法是什么导致它以及如何解决它?链接到以下工作和破坏的示例:
Working example with jQuery 1.11.3
Broken example with jQuery 2.2.4
HTML:
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
<li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
<li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
<li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">1...</div>
<div role="tabpanel" class="tab-pane" id="profile">2...</div>
<div role="tabpanel" class="tab-pane" id="messages">3...</div>
<div role="tabpanel" class="tab-pane" id="settings">4...</div>
</div>
JS:
$(document).ready(function() {
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href=#' + url.split('#')[1] + ']').tab('show');
}
// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function(e) {
window.location.hash = e.target.hash;
});
});
答案 0 :(得分:1)
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
}
这适合我。