422*_*422 56 jquery menu twitter-bootstrap
使用twitter bootstrap,我需要启动主导航的li部分的活动类.自动的.我们使用php而不是ruby.
示例导航:
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/forums">Forums</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/faqs.php">FAQ's</a></li>
<li><a href="/item.php">Item</a></li>
<li><a href="/create.php">Create</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
Bootstrap代码如下:
<li class="active"><a href="/">Home</a></li>
Run Code Online (Sandbox Code Playgroud)
因此,只需要弄清楚如何将活动类附加到当前页面.看了几乎所有关于Stack的答案,没有真正的快乐.
我玩过这个:
/*menu handler*/
$(function(){
var url = window.location.pathname;
var activePage = url.substring(url.lastIndexOf('/')+1);
$('.nav li a').each(function(){
var currentPage = this.href.substring(this.href.lastIndexOf('/')+1);
if (activePage == currentPage) {
$(this).parent().addClass('active');
}
});
})
Run Code Online (Sandbox Code Playgroud)
但没有快乐.
Imt*_*iaz 127
对于Bootstrap 3:
var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="'+ url +'"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function() {
return this.href == url;
}).parent().addClass('active');
Run Code Online (Sandbox Code Playgroud)
对于Bootstrap 4:
var url = window.location;
// Will only work if string in href matches with location
$('ul.navbar-nav a[href="'+ url +'"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.navbar-nav a').filter(function() {
return this.href == url;
}).parent().addClass('active');
Run Code Online (Sandbox Code Playgroud)
422*_*422 37
我们最终设法解决了:
/*menu handler*/
$(function(){
function stripTrailingSlash(str) {
if(str.substr(-1) == '/') {
return str.substr(0, str.length - 1);
}
return str;
}
var url = window.location.pathname;
var activePage = stripTrailingSlash(url);
$('.nav li a').each(function(){
var currentPage = stripTrailingSlash($(this).attr('href'));
if (activePage == currentPage) {
$(this).parent().addClass('active');
}
});
});
Run Code Online (Sandbox Code Playgroud)
Kam*_*Kam 30
你真的不需要任何带Bootstrap的JavaScript:
<ul class="nav">
<li><a data-target="#" data-toggle="pill" href="#accounts">Accounts</a></li>
<li><a data-target="#" data-toggle="pill" href="#users">Users</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
要在选择菜单项后执行更多任务,您需要JS,如此处其他帖子所述.
希望这可以帮助.
小智 13
如果你放
data-toggle="pill"
Run Code Online (Sandbox Code Playgroud)
在标签中它应该自动工作.
http://twitter.github.com/bootstrap/javascript.html#tabs
在标记下阅读
Seb*_*eck 10
这对我来说很有帮助,包括积极的主要下拉菜单和活跃的孩子们(感谢422):
$(document).ready(function () {
var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="' + url + '"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function () {
return this.href == url;
}).parent().addClass('active').parent().parent().addClass('active');
});
Run Code Online (Sandbox Code Playgroud)
如果您正在使用具有路由和操作的MVC框架:
$(document).ready(function () {
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');
});
Run Code Online (Sandbox Code Playgroud)
如Christian Landgren的回答所示:https://stackoverflow.com/a/13375529/101662
试试这个.
// Remove active for all items.
$('.page-sidebar-menu li').removeClass('active');
// highlight submenu item
$('li a[href="' + this.location.pathname + '"]').parent().addClass('active');
// Highlight parent menu item.
$('ul a[href="' + this.location.pathname + '"]').parents('li').addClass('active');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
143927 次 |
| 最近记录: |