2013-04-03 61 views
6

我有一项任务,在刷新页面时突出显示所选菜单。为此,我想使用cookie。 HTML代码如何将div的id设置为cookie?

<div class="menuBar"> 
      <div class="menuHeader ui-corner-top"> 
      <span><a href="#" onclick="Home()" id="home">Home</a></span> 
      </div> 
      <div class="menuHeader ui-corner-top"> 
      <span><a href="#" onclick="NewTransaction()" id="newtransaction">New Transaction</a></span> 
      </div> 
</div> 

JavaScript文件是

function Home() { 
      window.location.href = "../../home/welcome"; 
     } 
     function NewTransaction() { 
      window.location.href = "../../EnergyCatagory/index"; 
     } 

但是我有一个代码来设置菜单selected.But它不是一个好的way.How我可以通过选择菜单的值刷新时页面? 这是突出菜单

$(document).ready(function() { 
       var currentUrl = window.location.href; 
       if (currentUrl.indexOf("home/welcome") !== -1) { 
        $("#home").parent().parent().addClass('menuHeaderActive'); 
       } 
       else if (currentUrl.indexOf("EnergyCatagory/index") !== -1) { 
        $("#newtransaction").parent().parent().addClass('menuHeaderActive'); 
       } 
       else if (currentUrl.indexOf("portfolio/Index") !== -1) { 
        $("#portfolio").parent().parent().addClass('menuHeaderActive'); 
       } 
      }); 
+0

我编辑我的代码@MatthewAgustin – Niths

+0

@MatthewAgustin,可以任何代码是“非语义” ?对我来说,答案是否定的。山是非语义的,洗衣篮也是如此。 –

回答

2

您可以实现这样说:

function goToLocation(sLocation, id) { 
    $.cookie("activediv", id); 
    window.location.href = sLocation; 
} 

in html:

<a href="#" onclick="goToLocation('../../home/welcome', 'home')" id="home">Home</a> 
在jQuery的准备

$('#' + $.cookie("activediv")).parent().parent().addClass('menuHeaderActive'); 
+0

@ ChrisMoutray-在这里我得到了这样的类型错误的错误:$ .cookie不是一个函数 [打破这个错误] \t 风险价值=($饼干。 ( “activediv”));但我有cookie的插件 Niths

+0

@ victor-在这里我得到了这样的错误TypeError :$ .cookie不是函数[Break On This Error] var a =($。cookie(“activediv”));但我有cookie插件 Niths

+0

有关cookie与jQuery的链接,请参阅下面的注释。我只使用你的关于cookie的信息和信息来编译一些结果。 – Victor

4

给每菜单项的ID。

然后在菜单项的onclick()函数中将该id设置为cookie。

$.cookie("selectedId", $(this).attr('id')); 

在的document.ready功能添加选定类ID为从饼干得到的元件设置的Cookie