Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery-使用jquery ui accordion将活动类添加到导航链接构建中_Jquery_Accordion_Jquery Ui Accordion - Fatal编程技术网

jquery-使用jquery ui accordion将活动类添加到导航链接构建中

jquery-使用jquery ui accordion将活动类添加到导航链接构建中,jquery,accordion,jquery-ui-accordion,Jquery,Accordion,Jquery Ui Accordion,更新: 尝试对url使用regex,但效果良好: var url = window.location.pathname, urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); $("#accordion a").each(function() { if(urlRegExp.test(this.href.replace(/\/$/,''))) { $(this).addClass("active-sidebar

更新:

尝试对url使用regex,但效果良好:

var url = window.location.pathname, 
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");

$("#accordion a").each(function()
{
    if(urlRegExp.test(this.href.replace(/\/$/,'')))
    {
        $(this).addClass("active-sidebar-link");
    }
});
我正在使用jQueryUIAccordion小部件为我的网站构建一个侧栏导航菜单。

我有以下代码:

// create accordion
$( "#accordion" ).accordion(
{
    header: '> li, h3:not(> li > ul)',
    collapsible: true,
    autoHeight: false,
    navigation:true 
});

// Add active class to active sidebar links
$("#accordion a").each(function() 
{
    if (this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
    {
        $(this).addClass("active-sidebar-link");
    }
});
手风琴可以工作,“导航:正确”选项也可以工作(http://jqueryui.com/demos/accordion/#option-导航),它会根据您访问的链接打开相应的手风琴“抽屉”

但是,未将活动类添加到当前链接


有什么想法吗?

我会对href值执行搜索功能。如果您有多个页面的链接,那么您可以将代码进一步包括
window.location.pathname
,但以下是搜索域名的良好开端:

$("#accordion a").each(function() 
{
    if (this.href.search(window.location.hostname) != -1)
    {
        $(this).addClass("active-sidebar-link");
    }
});​

或者,正如我在上面的评论中所说,您可能需要使用正则表达式。

我认为您可能需要正则表达式。href=“”和href=“”之间的区别是什么?尝试使用regex时仍然不起作用。在问题中添加了regex代码。regex是赢家,谢谢!我在这个网站上使用的平台很烂,缓存没有更新,这就是为什么我第一次说它不工作的原因。Re:搜索解决方案,使我的所有链接都处于活动状态,因此在这种情况下没有什么好处,但将此标记为正确答案。