0

我不确定这个问题是否在其他地方被问过,但我在上面找不到任何东西,所以就到这里了。我目前正在托管一个网站,我使用一个小的、简单的 Javascript 函数来为某些列表项添加效果在我的导航栏上,具体取决于该功能是否能够找到地址中文本的某些匹配项。这是功能:

function NavBar(){
var address = (location.href);
    if(address.search("index")!==(-1)){
    document.getElementById('Nav-Home').firstChild.setAttribute('style', 'color: #cecece');
}
    if(address.search("work")!==(-1)){
    document.getElementById('Nav-Work').firstChild.setAttribute('style', 'background-color: #E5E5E5;');
}
    if(address.search("library")!==(-1)){
    document.getElementById('Nav-Tutorial').firstChild.setAttribute('style', 'background-color: #E5E5E5;');
}
    if(address.search("contact")!==(-1)){
    document.getElementById('Nav-Contact').firstChild.setAttribute('style', 'background-color: #E5E5E5;');
}
    if(address.search("about")!==(-1)){
    document.getElementById('Nav-About').firstChild.setAttribute('style', 'background-color: #E5E5E5;');
}}

因此,我的页面存储在每个文件夹名称(Work、Tutorial、Contact 和 About)下的网站目录中。但是,无论我将 index.html 文件存储在单独的文件夹中,还是我似乎无法找到以使“索引”一词出现在站点地址中的任何内容(我的目标是类似http://site .com/index.html)。

有没有人可以将 index.html 添加到默认主页而不是将其切断,或者更改我的功能?

非常感谢

4

0 回答 0