【发布时间】:2017-07-18 15:35:57
【问题描述】:
我正在尝试使用 jQuery 更改 Web 应用程序上所有 <li> 链接的颜色,但不知道为什么它不起作用?
我的 layout.cshtml 页面顶部有我的样式声明,底部有 jQuery 函数。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Contoso University</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<style>
li {color: blue;}
.emphasis {color: indianred;}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Contoso University", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
@if (User.Identity.IsAuthenticated)
{
if (!User.IsInRole("Instructor"))
{
<li>@Html.ActionLink("Students", "Index", "Student")</li>
}
<li>@Html.ActionLink("Courses", "Index", "Course")</li>
<li>@Html.ActionLink("Instructors", "Index", "Instructor")</li>
if (!User.IsInRole("Student") && !User.IsInRole("Instructor"))
{
<li>@Html.ActionLink("Departments", "Index", "Department")</li>
}
if (!User.IsInRole("Student"))
{
<li>@Html.ActionLink("Enrollments", "Index", "Enrollment")</li>
}
}
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - Contoso University</p>
</footer>
</div>
@*<script>
$('li').addClass('emphasis');
</script>*@
<script>
$(function () {
$('li').addClass('emphasis');
});
</script>
</body>
</html>
我做错了什么?
【问题讨论】:
-
你能显示你的 li 标记吗?
-
你是否在你的页面中添加了 jquery 库
-
是的,它是最新版本。
-
问题是在页面引导过程中 jquery 无法处理这些标签
-
您正在为
li中的链接着色。您的 css 应该是li a { }而不仅仅是li { },请参阅 stackoverflow.com/questions/1585855/…
标签: javascript jquery html css asp.net-mvc-5