Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 无法更改当前的css类<;导航>;_Jquery_Css_Asp.net_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Jquery 无法更改当前的css类<;导航>;

Jquery 无法更改当前的css类<;导航>;,jquery,css,asp.net,asp.net-core,asp.net-core-mvc,Jquery,Css,Asp.net,Asp.net Core,Asp.net Core Mvc,我的asp.net MVC核心web应用程序中有以下:- <nav> <ul id="navigation"> <li class="@Html.IsSelected(actions: "Home", controllers: "Default")"><a href="/">home</a></li> <

我的asp.net MVC核心web应用程序中有以下
:-

<nav>
      <ul id="navigation">
                          <li class="@Html.IsSelected(actions: "Home", controllers: "Default")"><a href="/">home</a></li>
                          <li class="@Html.IsSelected(actions: "FAQ", controllers: "Default")"><a href="/home/FAQ">FAQ</a></li>
                          <li class="@Html.IsSelected(actions: "Contact", controllers: "Default")"><a href="/home/contact/">Contact</a></li>
      </ul>
</nav>

我想更改当前链接的类,我定义了以下html扩展方法:-

public static class HtmlHelperExtensions
    {
        public static string IsSelected(this IHtmlHelper htmlHelper, string controllers, string actions, string cssClass = "selected")
        {
            string currentAction = htmlHelper.ViewContext.RouteData.Values["action"] as string;
            string currentController = htmlHelper.ViewContext.RouteData.Values["controller"] as string;

            IEnumerable<string> acceptedActions = (actions ?? currentAction).Split(',');
            IEnumerable<string> acceptedControllers = (controllers ?? currentController).Split(',');

            return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
                cssClass : String.Empty;
        }
    }
公共静态类HtmlHelperExtensions
{
已选择公共静态字符串(此IHtmlHelper htmlHelper、字符串控制器、字符串操作、字符串cssClass=“已选择”)
{
字符串currentAction=htmlHelper.ViewContext.RoutedData.Values[“action”]作为字符串;
字符串currentController=htmlHelper.ViewContext.RoutedData.Values[“controller”]作为字符串;
IEnumerable acceptedActions=(actions??currentAction).Split(',');
IEnumerable AcceptedController=(控制器??currentController).Split(',');
返回acceptedActions.Contains(currentAction)和&AcceptedController.Contains(currentController)?
cssClass:String.Empty;
}
}
但是当我点击任何
链接时,它们都没有得到任何特效!有什么建议吗


感谢调试以检查IsSelected方法是否返回cssClass。从代码中,当当前url为以下url之一时,它将仅返回cssClass:

  • /默认/主
  • /默认/常见问题解答
  • /默认/联系方式
此外,
  • 标记 似乎没有一个内置的
    选定的
    csc类,您需要在某个地方定义它

    我根据您的代码进行了测试,只需更改颜色:

    在_Layout.cshtml中

    <nav>
    <ul id="navigation">
        <li style="@Html.IsSelected(actions: "Index", controllers: "Home")"><a href="/">Home</a></li>
        <li style="@Html.IsSelected(actions: "FAQ", controllers: "Home")"><a href="/home/FAQ">FAQ</a></li>
        <li style="@Html.IsSelected(actions: "Contact", controllers: "Home")"><a href="/home/contact/">Contact</a></li>
    </ul>
    </nav>
    
    
    
    Html扩展名:

    public static class HtmlHelperExtensions
    {
        public static string IsSelected(this IHtmlHelper htmlHelper, string controllers, string actions, string cssClass = "color:red")
        {
            string currentAction = htmlHelper.ViewContext.RouteData.Values["action"] as string;
            string currentController = htmlHelper.ViewContext.RouteData.Values["controller"] as string;
    
            IEnumerable<string> acceptedActions = (actions ?? currentAction).Split(',');
            IEnumerable<string> acceptedControllers = (controllers ?? currentController).Split(',');
    
            return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
                cssClass : String.Empty;
        }
    }
    
    公共静态类HtmlHelperExtensions
    {
    选择公共静态字符串(此IHtmlHelper htmlHelper、字符串控制器、字符串操作、字符串cssClass=“color:red”)
    {
    字符串currentAction=htmlHelper.ViewContext.RoutedData.Values[“action”]作为字符串;
    字符串currentController=htmlHelper.ViewContext.RoutedData.Values[“controller”]作为字符串;
    IEnumerable acceptedActions=(actions??currentAction).Split(',');
    IEnumerable AcceptedController=(控制器??currentController).Split(',');
    返回acceptedActions.Contains(currentAction)和&AcceptedController.Contains(currentController)?
    cssClass:String.Empty;
    }
    }
    
    结果: