简体   繁体   English

从活动菜单项javascript开始

[英]start with active menu item javascript

Hi I'm new and not sure if I'm doing this correctly. 嗨,我是新手,不确定我是否做对了。

I use Javascript that will decorate an active link after it's been clicked. 我使用的Javascript将在点击后装饰一个活动链接。 Question is, how can I load the page with one of the menu items already active? 问题是,如何加载已激活的菜单项之一的页面?

Example: http://moschalkx.nl/ 示例: http//moschalkx.nl/

Javascript code: JavaScript代码:

function hlite_menu(obj){
    var lnk=document.getElementById('menu').getElementsByTagName('A');
    for(var i in lnk){
        lnk[i].className=(lnk[i]===obj)?'menu_active':'menu_idle';
    }
}

function set_menu(){
    var lnk=document.getElementById('menu').getElementsByTagName('A');
    for(var i in lnk){
        lnk[i].className='menu_idle';
        lnk[i].onclick=function(){
            hlite_menu(this);
        }
    }
}

window.onload=set_menu;

CSS: CSS:

a.menu_idle {color:#333333; text-decoration:none;}
a.menu_active {color:#333333; text-decoration:underline;}
a:visited {color:#333333; text-decoration:none;}
a:hover {color:#333333; text-decoration:underline;}

You already have your hlist_menu function that sets a particular link to be active, so I would just call that from your set_menu function for whichever link is supposed to be active to begin with 您已经有了hlist_menu函数,该函数可以将特定链接设置为活动状态,所以我只需要从set_menu函数中调用该链接,就应该以哪个链接应该处于活动状态

function set_menu(){
    var lnk=document.getElementById('menu').getElementsByTagName('A');
    for(var i in lnk){
      lnk[i].className='menu_idle';lnk[i].onclick=function({hlite_menu(this);}}

      if (lnk[i] /* ??? how do you know whether this is the link to activeate up front? */ ) {
          hlist_menu(lnk[i]);
      }
    }

Also, this 还有这个

lnk[i].onclick=function({hlite_menu(this);}}

can be simplified to just 可以简化为

lnk[i].onclick = hlite_menu;

assuming you change it to 假设您将其更改为

function hlite_menu(){
    var lnk= document.getElementById('menu').getElementsByTagName('A');
    for(var i in lnk){
       lnk[i].className = (lnk[i] === this) ? 'menu_active':'menu_idle';
    }
}

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

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