简体   繁体   English

如何将当前页面的网址与导航栏匹配?

[英]How to match current page url with nav bar?

I need to compare the url of the page I am on to the li element in my nav bar, Using jQuery. 我需要将导航到的页面的URL与导航栏(使用jQuery)中的li元素进行比较。

So far I have the current url assigned to a variable, like so 到目前为止,我已经将当前网址分配给了一个变量,就像这样

var currentPage = window.location.href;

then I have a .each function checking each item in the nav bar, like so 然后我有一个.each函数,检查导航栏中的每个项目,像这样

$("nav ul li a").each(
    function() {

    }
);

Now, I assume I need some kind of if statement inside the function, maybe something like this? 现在,我假设我需要在函数内部使用某种if语句,也许是这样的?

if(currentPage === ????) {};

What I can't figure it out is what to compare the url to from the list? 我不知道是什么比较列表中的url?

Basically I want whatever page I'm on to be highlighted in the nav bar. 基本上,我想在导航栏中突出显示我正在浏览的任何页面。 Once I figure out how to get jQuery to acknowledge what page I'm on and match it up with the nav bar, I will use .css to change the background image to make which ever page i'm on stand out on the nav bar. 一旦我弄清楚如何让jQuery确认我在哪个页面上并将其与导航栏匹配,我将使用.css更改背景图像,以使我站在导航栏上的哪个页面脱颖而出。 I am in a jQuery class and this is something that has been assigned. 我在jQuery类中,这已分配。

I searched around on here and I couldn't find anything to really match was I'm trying to do. 我在这里四处搜寻,但找不到我要尝试的真正匹配的东西。

Hopefully someone has some insight...if this question even makes sense, I'm practically pulling out my hair just trying to word this into a question, much less figure it out... 希望有人能提供一些见识...如果这个问题甚至有意义,我实际上是在拔头发,只是想将其表达为一个问题,更不用说弄清楚了...

I think you might be looking for: 我认为您可能正在寻找:

this.href

That should give you the actual href of the a tag you're looping over. 那应该给您您正在循环的标签的实际href。

(Answer improved, thanks to comment below) (答案有所改善,感谢以下评论)

you can use parse(..) to match link whether is current page.the demo can't run correctly in stackoverflow,if you want to see the result,you should go codepen . 您可以使用parse(..)来匹配链接是否为当前页面。该演示无法在stackoverflow中正确运行,如果要查看结果,则应转到codepen

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style> a { color: #000; } a.active { color: red; } </style> <nav> <ul> <li><a href="?home">Home</a></li> <li><a href="?contact">Contact</a></li> <li><a href="?about">About</a></li> <li><a href="?contact#hash">Contact Us</a></li> </ul> </nav> <script> function parse(url) { var parts = url.match(/^([^#?]*?)?(\\?.*?)?(#.*)?$/); return { uri: parts[1], search: parts[2], hash: parts[3], /** * * @param that <b>string</b>/<b>object</b> has `href` property,eg:location * @param hash skip hash matching * @param search skip search matching * @returns {boolean|*} if url matched return `true`,otherwise return `false`. */ match: function (that, hash, search) { var self = this, that = parse(typeof that == 'string' ? that : that.href); return (!self.uri || self.uri == that.uri) && (search || self.search == that.search) && (hash || self.hash == that.hash); } }; } $('nav li a').each(function () { console.log(['matching with ', this.href, '===>', parse(this.href).match(location)].join(' ')); console.log(['matching with ', $(this).attr('href'), '===>', parse($(this).attr('href')).match(location)].join(' ')); if (parse($(this).attr('href')).match(location/*,true*/))//remove comment and try the result. $(this).addClass('active'); }); </script> 

For anyone that looks at this I got it figured out using this and .each. 对于任何看这个的人,我都知道使用this和.each。 Here's what it looks like. 这是它的样子。

var currentPage = window.location.href; var currentPage = window.location.href;

$("nav ul li a").each(
    function() {
        console.log($(this).attr("href"));
        if (currentPage.indexOf($(this).attr("href")) !== -1){
            $(this).addClass("highlight");
            return false;
        }
     }
);

I was WAY off when I first tried tackling this, I think I misunderstood my teacher and was over thinking the problem. 当我第一次尝试解决这个问题时,我还很遥远,我想我误解了我的老师,并且正在思考这个问题。 Anyway this works great. 无论如何,这很好。

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

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