2011-11-24 38 views
3

以下代码显示了我的div实现的侧面菜单。JQuery HTML Div导航

<div class="top_link"> 
    <h3><a href="/dashboard/" class="dash_board_link">Dashboard</a></h3> 
</div> 
<div id="accordion" class="accordion_menu"> 
    <h3><a href="#section1">Hits</a></h3> 
    <div class="content"> 
     <a href="/dailyhits/">Daily Hits</a> 
     <a href="/tophundredurls/?page=1">Top 100 URL</a> 
    </div> 
</div> 
<div class="bottom_link"> 
    <h3><a href="/userwatchlist">Watch Lists</a></h3> 
</div> 
<div class="bottom_link"> 
    <h3><a href="/twitterinsights">Twitter Insights</a></h3> 
</div> 
<div class="bottom_link selected"> 
    <h3><a href="/managedomain"> Manage Domain </a></h3> 
</div> 

使用jQuery我想读取当前的URL,它修剪到这是在href属性指定的格式,如果有比赛我想具体div元素的选定部分添加到div class="xxx select"。为了做到这一点,我添加了下面的jQuery代码:

$(document).ready(function() { 
    var pathname = window.location.pathname; 
}); 

我不知道如何继续进行,因为这样庵新的jQuery。

回答

2

有没有尝试过,但沿着这些lings东西应该工作:

var pathname = window.location.pathname; 
var pathPart = pathname.slice('.com/', '/'); // assuming this is the end of your url 
$('#navigation a').click(function(){ 
    var url = $(this).attr('href'); 
    $('#navigation a').removeClass('active'); 
    if (pathPart == url) { 
     $(this).addClass('active'); 
    } 
}); 
+2

一个注意事项 - 即在ie中(我认为最高版本为8)href属性包括完整的URL,包括主机,即使它不是你使用jQuery输入/设置的内容。考虑到这一点可能会很棘手(尽管最近的jQuery版本可能已经考虑到了这一点)。你可能应该在init脚本中做一些功能检测(创建一个虚拟的隐藏链接,将其url设置为“/”,然后检查.attr(“href”)返回的url是否等于“/” ) – wheresrhys

+0

+1很好的回答和评论 – amelvin

2

使用jQuery基本上涉及在屏幕上选择某些东西(例如div),然后对其执行操作(例如替换其文本)。

所以你的jquery需要对你设置的路径名var做些什么。

而且你jQuery是不是很有效,你的失踪到底几个字符:

$(document).ready(function() { 
    var pathname = window.location.pathname; 
    // select something here and use the pathname, eg: 
    $(".bottom_link").append(pathname); 
}); 

但是,从你的描述我真的不知道你想要的选择,或者你想要什么用它做 - 但希望这会让你开始?