jQuery Selector Step By Step Tutorial - Part 2: In this post, we want to modify the top level items, and only the top level item. We arrange become horizontal. For this job, create a category class in the stylesheet.
Create a file named "song.css". Enter following code:
@charset "utf-8";
/* CSS Document */
.category{
float:left;
list-style:none;
margin: 10px;
}
Then, we write jQuery code. Create a file named "song.js". Enter following code:
// JavaScript Document
$(document).ready(function(){
$('#favorite > li').addClass('category');
});
Before try, attach "jquery.js", "song.css", and "song.js" at your HTML header.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Beatles Song</title>
<link rel="stylesheet" href="song.css" type="text/css" media="screen">
<script src="jquery.js" type="text/javascript"></script>
<script src="song.js" type="application/javascript"></script>
</head>
<h1>My Favorite Beatles Song</h1>
<div id="description">These is all of my favorite song from The Beatles. How about you?</div>
<ul id="favorite">
<li>Love
<ul>
<li><a href="love.html">Love me do</a>
<li>Something
<li>Here, There, and Anywhere
</ul>
<li>Balads
<ul>
<li><a href="obladi.pdf">Obladi Oblada</a>
<li>Get Back
<li>Help
</ul>
<li>Rock 'n Roll
<ul>
<li><a href="mailto:music.rock">Rock 'n Roll The Music</a>
<ul>
<li>Part 1
<li>Part 2
</ul>
<li>Hard Days Night
<li>Standing There
</ul>
</ul>
<body>
</body>
</html>
Point your browser to this practice address.