ASP.NET MVC:母版:如何设置CSS类的活动菜单项 [英] ASP.NET MVC: Masterpage: How to set css class on active menu item

查看:153
本文介绍了ASP.NET MVC:母版:如何设置CSS类的活动菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的菜单在我的母版:

 < UL ID =菜单级=lavaLampBottomStyle>
    <立GT;
        <%= Html.ActionLink(员工,索引,员工)%GT;< /李>
    <立GT;
        <%= Html.ActionLink(客户,详细信息,帐户)%GT;< /李>
< / UL>

我需要一种方法来设置css类当前活动Li与当前。

我的第一个猜想它使用JavaScript的协助下做到这一点。

我将包括在母版是这样的:

  $(#菜单里一)各(){
    如果($(本).attr(的href)=='<%= *获取当前PAGE *%GT;'){
       $(本).parent(「李先生」)addClass(电流)。
    }
  }

这是一个很好的方法?

如果是这样,我怎么能得到当前的URL部分像在href?

如果不是的话,你有些什么建议? : - )

仅供参考,生成的HTML后我:

 < UL ID =菜单级=lavaLampBottomStyle>
    <立GT;
        < A HREF =/ KszEmployees /索引>员工与LT; / A>< /李>
    <立GT;
        <一类=当前的href =/>客户与LT; / A>< /李>
< / UL>


解决方案

这可能是做这件事的至少密集的方式。如果你能在用户数有启动Javascript,我看没有错,我已经这么做了自己有时。

Request.Url是你有兴趣获得服务器端的当前页面的对象。由tvanfosson使用window.location.href的建议也不错,如果你想保持它完全是客户方。

使用服务器端的优势,是Request.Url具有URL的方便部分,如Request.Url.Host等,以帮助您链接改写(munging)的需求。

I have the following menu in my masterpage:

<ul id="menu" class="lavaLampBottomStyle">
    <li>
        <%= Html.ActionLink("Employees", "Index", "Employees")%></li>
    <li>
        <%= Html.ActionLink("Customer", "Details", "Account")%></li>
</ul>

I need a way to set the css class of the current active li to "current".

My first guess it to do this with the assistance of javascript.

I would include something like this in the masterpage:

  $("#menu li a").each(){
    if($(this).attr("href") == '<%= *GET CURRENT PAGE* %>'){
       $(this).parent("li").addClass("current");
    }
  }

Is this a good approach?

If it is, how can I get the current URL part like in the href?

If it isn't, what's your suggestion? :-)

FYI, the generated html I'm after:

<ul id="menu" class="lavaLampBottomStyle">
    <li>
        <a href="/KszEmployees/Index">Employees</a></li>
    <li>
        <a class="current" href="/">Customer</a></li>
</ul>

解决方案

That's probably the least intensive way of doing it. If you can count on the users to have javascript enabled, I see nothing wrong with this, and have done it myself on occasion.

Request.Url is the object you are interested in to get the current page on the server side. The suggestion to use window.location.href by tvanfosson isn't bad either if you want to keep it entirely clientside.

The advantage of using serverside, is that Request.Url has easily accessible parts of the url, such as Request.Url.Host, etc to help with your link-munging needs.

这篇关于ASP.NET MVC:母版:如何设置CSS类的活动菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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