jQuery 101 - Change CSS not working with advanced selectors
This is across IE and Firefox.
I have an unordered list of 5 items within li tags.
<script type="text/javascript">
$(function() //when doc is ready
{
$("li:first").css('color', 'red');
});
</script>
That does absolutely nothing, however if I leave out the :first part, it makes all li's red, unsure of why since I'm following a tutorial.
Here is the html:
<div id="container">
<script type="text/javascript"><!--
$(function() //when doc is ready
{
$("li:first").css('color', 'red');
});//--></script>
<div id="leftcontent">
<center><img src="http://www.dmu.com/5f/images/thiszis.png"><开发者_开发技巧/center>
<div id="hoastdears">
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
With
in the header.
Specify a context for your li:first
selector. Something like:
$("li:first", "ul:first").css("color", "red");
Try this
$(document).ready(function(){
$("li:first").css('color', 'red');
});
Here is a link to the jQuery API http://api.jquery.com/ready/
It works for me as is, the problem must be somewhere else:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript"><!--
$(function() //when doc is ready
{
$("li:first").css('color', 'red');
});//--></script>
</head>
<body>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</body>
</html>
Update: Find your browser's JavaScript console (in Firefox, it can be found under the Tools menu). You might have a syntax error somewhere that's preventing JavaScript code from executing.
精彩评论