Reputation: 469
I am trying to change the color of all the <li>
links on web app using jQuery, but don't know why it is not working?
I have my style statement at the top in my layout.cshtml page, and the jQuery function at the bottom.
<!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>
What is it that I am doing wrong?
Upvotes: 2
Views: 579
Reputation: 469
So, after a lot of trials & errors, found out I was using the wrong selector. Posting the answer just in case if someone else has this issue.
<script>
$('.navbar-inverse .navbar-nav > li > a').css('color', 'red');
</script>
Upvotes: 1
Reputation: 1546
Here are a few options...
JavaScript
If you're trying to color the bullet, working jsfiddle
$('li').addClass('emphasis');
If you're trying to color the hyperlink, working jsfiddle
$('li a').addClass('emphasis');
If you're trying to color both the bullet and the hyperlink, working jsfiddle
$('li a, li').addClass('emphasis');
HTML
<li><a href="http://www.google.com">TEST 1</a></li>
<li><a href="http://www.google.com">TEST 2</a></li>
<li><a href="http://www.google.com">TEST 3</a></li>
<li><a href="http://www.google.com">TEST 4</a></li>
<li><a href="http://www.google.com">TEST 5</a></li>
CSS
li {color: blue;}
.emphasis {color: indianred;}
Upvotes: 2
Reputation: 76577
Ensure that you wrap your <script>
code within a document-ready block so that it executes when your page (and dependencies) have been loaded :
<script>
// This will not execute until jQuery (and your page) have been loaded
$(function(){
$('li a').addClass('emphasis');
});
</script>
Upvotes: 0
Reputation: 45
try to make a static html
<li>test</li>
if it works , that means your Jquery code fired before your html li is generated.
Upvotes: -2