简体   繁体   English

如何将JS用于“活动页面”状态类

[英]How to use JS for “active page” status class

My website uses a css navigation similar to this tutorial, http://net.tutsplus.com/tutorials/html-css-techniques/creating-a-floating-html-menu-using-jquery-and-css/ 我的网站使用的CSS导航类似于本教程, http://net.tutsplus.com/tutorials/html-css-techniques/creating-a-floating-html-menu-using-jquery-and-css/

It's a single page website. 这是一个单页网站。

I need to make it so when you click a link and it scrolls down to it's relevant content, that the my class named "active" will be applied to the markup. 我需要这样做,以便当您单击链接并将其向下滚动到其相关内容时,名为“ active”的我的类将应用于标记。

This is what I have attempted so far: 到目前为止,这是我尝试过的:

aObj = document.getElementById('navigation').getElementsByTagName('a');
      for(i=0;i<aObj.length;i++) {
        if(document.location.href.indexOf(aObj[i].href)>=0) {
          aObj[i].className='active';
        }
      }
    }
    window.onload = function() {
    if (window.location.hash.length > 1) {
        page = window.location.hash.split("#")[1];
        load(page);
    }

}

I don't know JS and this is out of my comfort zone. 我不认识JS,这超出了我的舒适范围。 Thanks 谢谢

EDIT 编辑

A friend pointed me to this website http://imakewebthings.github.com/jquery-waypoints/ . 一个朋友将我指向该网站http://imakewebthings.github.com/jquery-waypoints/

A brief overview of how my page is setup: 我的页面设置的简要概述:

<div id="home">
  <h1>Homepage</h1>
</div>
<div id="portfolio">
  <h1>Portfolio</h1>
</div>
<div id="contact">
  <h1>Contact Us</h1>
</div>
<div id="navigation">
  <ul>
    <li><a href="#home">home</a><li>
  </ul>
</div>

It still will not add the class to the markup, any suggestions? 它仍然不会将类添加到标记中,有什么建议吗?

One adjustment I will point out - you set navigation elements to "active" class, but never remove it. 我要指出的一项调整-您将导航元素设置为“活动”类,但从不删除它。 This will take care of that: 这将解决以下问题:

aObj = document.getElementById('navigation').getElementsByTagName('a');
for(i=0;i<aObj.length;i++)
{
    if(document.location.href.indexOf(aObj[i].href)>=0)
    {
        aObj[i].className='active';
    }
    else
    {
        aObj[i].className='inactive';
        //or set to '', or whatever your default is    
    }
}

Something like this? 像这样吗

window.onload = function() {
    aObj = document.getElementById('navigation').getElementsByTagName('a');
    for(i=0;i<aObj.length;i++)
    {
        aObj[i].onclick = function()
        {
            //alert(this.href.split("#")[1]);
            load(this.href.split("#")[1]);
        }
    }

    if (window.location.hash.length > 1) {
        page = window.location.hash.split("#")[1];
        load(page);
    }
}
function load(p)
{
    aObj = document.getElementById('navigation').getElementsByTagName('a');
    for(i=0;i<aObj.length;i++)
    {
        aObj[i].className = (p==aObj[i].href.split("#")[1]) ? 'active':'';
    }
}

Just to be clear, you are calling the code that adds the "active" class to the link in the onclick of all links, right? 为了清楚起见,您正在调用在所有链接的onclick中将“活动”类添加到链接的代码,对吗? Because that's what's missing from your example code. 因为这就是示例代码所缺少的。 It simply runs through all links once. 它仅通过所有链接运行一次。 So in the current state, when a user clicks a link, your javascript code doesn't run. 因此,在当前状态下,当用户单击链接时,您的JavaScript代码不会运行。

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

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