【问题标题】:Change color of <li> in <ul>改变 <ul> 中 <li> 的颜色
【发布时间】:2017-10-28 23:01:41
【问题描述】:

我在&lt;ul&gt; 中有一些&lt;li&gt; 的代码

这里是

 <ul class="nav navbar-nav navbar-right" style="color:black;">
    <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
    <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>

我需要将&lt;li&gt;的颜色改为黑色

我尝试这样做style="color:black;"

这是 ASP.NET 代码

这是生成的代码

<ul class="nav navbar-nav navbar-right">
    <li style="color:#000000;"><a href="/Account/Register" id="registerLink">Register</a></li>
    <li style="color:#000000;"><a href="/Account/Login" id="loginLink">Log in</a></li>
</ul>

但颜色没有改变。

我怎样才能改变它?

【问题讨论】:

  • 那是 ASP 吗?请显示呈现的 HTML。看起来这些会生成a 标签,所以您需要将样式应用于a 或使用.nav a { color: black; } 之类的东西
  • 是的,现在会更新帖子@MichaelCoker
  • 你想试试 htmlAttributes: new {@style='color:black'} 在你的 >@Html.ActionLink

标签: jquery html css asp.net asp.net-mvc


【解决方案1】:

在 ActionLink 的 'htmlAttributes' 中添加类。

@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink", @class = "aclass" })

在你的 CSS 中:

/* Class of 'a' element
.aclass {
      color: black;
      ......
}

这应该可行。

编辑: 您必须使用“@”,因为 class 是 C# 中的关键字。 链接到 MSDN 文档:

Documentation

【讨论】:

  • 尝试使用 '@class= "aclass"' insted of 'class= "aclass"'。在类 attr 之前添加一个“@”。 (我已经编辑了我的答案)@s.e.
【解决方案2】:

这应该可行:

 <ul class="nav navbar-nav navbar-right">
    <li style="color:black;">@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
    <li style="color:black;">@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>

但是,由于您在 &lt;li&gt; 中呈现锚标记,因此它们可能不是黑色的。由于您无法将样式属性添加到锚标记,因此您必须添加此 CSS:

ul.navbar-nav a {
    color: #000;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多