根据位置高亮显示一个按钮 [英] Highlight a button based on location

查看:126
本文介绍了根据位置高亮显示一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据正在查看的页面突出显示导航按钮(在菜单中).这是我到目前为止的内容:

I'm trying to highlight a navigational button (in a menu) based on the page being viewed. Here is what I have so far:

var loca = String(document.location.href);

// Get document location and specific page.
if (loca) {
    if(loca.search(RegExp("((/[\w]*)\.php)")) != -1) {
        activate(loca.match(RegExp("((/[\w]*)\.php)").split("/").join("")));
    } else {
        activate("home");
    }
}

// Activate a button
function activate(bName) {
    $(".button[name=" + bName + "]").css({
        "border-left": "1px solid white",
        "border-right": "1px solid white"
    });
}

我想发生的是这样:

  1. 获取页面的网址
  2. 获取页面的特定文件名,如果找不到,则位于主页上.
  3. 我使用jQuery,尝试找到按钮的名称,如果名称与文件名匹配,则将其突出显示.

事实是,这仅突出显示主页"按钮.我究竟做错了什么?另外,如果您对如何更好地完成此操作有任何建议,请告诉我!

Thing is, this only highlights the "Home" button. What am I doing wrong? Also, if you have any suggestions on how I can better accomplish this, please let me know!

推荐答案

我会这样获得文件名:

var pathname = window.location.pathname.split("/");
var filename = pathname[pathname.length-1].split(".")[0];

alert(filename);

这篇关于根据位置高亮显示一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆