简体   繁体   English

将类添加到父级LI,其中A =当前文档?

[英]Add class to parent LI where A = current document?

I'm trying to append a class to an LI automatically if the filename matches the href (eg; if the anchor has the href attribute set to about.php and the filename of the current page is about.php then it would append the class.) however, along the way I've hit some complications and I've been getting myself confused with the syntax a little... 我正在尝试自动将类添加到LI,如果文件名与href匹配(例如;如果锚点的href属性设置为about.php,而当前页面的文件名是about.php,那么它将添加该类。)但是,在此过程中,我遇到了一些麻烦,并且使自己对语法有些困惑...

So far I have this... 到目前为止,我有这个...

var filename = location.pathname.substring(1)

$(document).ready(function(){
    $('a').each(function() {
        if (this.href === filename) {
            $(this).parent('li').addClass('current_page_item');
        }
    });
});

My navigation is constructed as shown here and as you can see, it works and everything but only when the class is set manually... so I'm trying to get it to be automatic from the actual filename. 我的导航的构造如图在这里和你可以看到,它的工作原理和一切,但只有当阶级手动设置......所以我试图得到它与实际文件名自动。

I hope this make some sense to someone, as I'm really confused on how to get this working now! 我希望这对某人有意义,因为我对现在如何使它正常工作感到困惑!

Thank-you to anyone who contributes, it's of great help and will help me understand the jquery syntax further, actually selecting something specific and writing a logical statement in jquery really confuses me as it's very different to PHP, which is what I'm used to. 谢谢任何贡献者,它有很大的帮助,它将帮助我进一步了解jquery语法,实际上选择特定的东西并在jquery中编写逻辑语句确实使我感到困惑,因为它与PHP截然不同。至。

My markup looks like this, since I didn't include it before and just assumed people would look at the source code to understand what I was meaning (though, I should have put it here) 我的标记看起来像这样,因为我以前没有包含它,只是假设人们会看一下源代码来理解我的意思(尽管我应该把它放在这里)

<nav>
    <div class="container">
        <ul class="group" id="effects">
            <li><a href="index.php" title="View the latest news">News</a></li>
            <li><a href="about.php" title="Find out more about us">About</a></li>
            <li><a href="#" title="Find out about races">Races</a></li>
            <li><a href="#" title="Find out what tools we use">Tools</a></li>
            <li><a href="#" title="Read the Frequently Asked Questions">FAQ</a></li>
            <li><a href="contactus.php" title="Contact us">Contact Us</a></li>
        </ul>
    </div>
</nav>

Thanks. 谢谢。

I think you can do it as simple as this: 我认为您可以像这样简单:

$(function(){
    $('a').filter(function() {
      return (this.href == location.href);
    }).parent('li').addClass('current_page_item');
});

​Filter the anchor tags by their href attribute. 按锚标签的href属性过滤锚标签。 Seems to work on this JSFiddle . 似乎可以在此JSFiddle上工作。 For some reason, you have to hit run for it to work. 出于某种原因,您必须点击运行才能使其正常运行。

I assume that li tag is parent of anchor tag than you can try this- 我认为li标签是锚标签的父标签,您可以尝试以下操作-

$(this).parent().addClass('current_page_item');

instead: 代替:

$(this).parent('li').addClass('current_page_item');

Based on what little information you've provided in your question, I'd suggest (though this is untested): 根据您在问题中提供的信息很少,我建议您(尽管这未经测试):

var file = document.location.href.split('/').pop();
$('li a').filter(function(){
    return this.href.indexOf(file) !== -1;
}).parent().addClass('current_page_item');

This gets the contents of the current page's URL and splits it by the / character and then assigns the last element of the array to the variable file , for example: 这将获取当前页面URL的内容,并用/字符将其分割,然后将数组的最后一个元素分配给变量file ,例如:

'http://example.com/directory/page.html'

is converted into an array: 转换为数组:

["http:", "", "example.com", "directory", "page.html"]

And the last element of that array is assigned to the variable, so file becomes equal to: 并将该数组的最后一个元素分配给变量,因此file等于:

'page.html'

The jQuery iterates over every a element within an li element and then, if it finds the string page.html within the href attribute it returns that a element, and adds the class-name to its parent element. jQuery的遍历每a的元素内li元素,然后,如果找到字符串page.html的内href属性它返回a元素,并添加类名父元素。

You could, instead, use the full document.location.href , of course; 当然,您可以使用完整的document.location.href which would give: 这将给:

var page = document.location.href;
$('li a').filter(function(){
    return this.href == page;
}).parent().addClass('current_page_item');

This works much the same way, but instead of looking to see if the filename can be found within the URL, it checks that the href of the element is exactly equal to the current document.location.href . 这样做的方式几乎相同,但是它没有检查是否可以在URL中找到文件名,而是检查元素的href是否与当前document.location.href 完全相等。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM