FreeCodeCamp(Part 3)
- Create a Bootstrap Headline
To start with, create an
h3
element, with the text jQuery Playground.
Color yourh3
element with thetext-primary
Bootstrap class, and center it with thetext-center
Bootstrap class.
- House our page within a Bootstrap Container Fluid Div
Now let's make sure all the content on your page is mobile-responsive.
Let's nest yourh3
element within adiv
element with the classcontainer-fluid
.
- Create a BootStrap Row
Now we'll create a Bootstrap row for our inline elements.
Create adiv
element below theh3
tag, with a class ofrow
.
- Split your Bootstrap Row
Now that we have a Bootstrap Row, let's split it into two columns to house our elements.
Create twodiv
elements within yourrow
, both with the classcol-xs-6
.
- Create Bootstrap Wells
Bootstrap has a class called
well
that can create a visual sense of depth for your columns.
Nest onediv
element with the classwell
within each of yourcol-xs-6
div elements.
- Add Elements within your Bootstrap Wells
Now we're several
div
elements deep on each column of our row. This is as deep as we'll need to go. Now we can add ourbutton
elements.
Nest threebutton
elements within each of yourwell
div elements.
- Apply the Default Bootstrap Button Style
Bootstrap has another button class called
btn-default
.
Apply both thebtn
andbtn-default
classes to each of yourbutton
elements.
- Create a Class to Target with jQuery Selectors
Not every class needs to have corresponding CSS. Sometimes we create classes just for the purpose of selecting these elements more easily using jQuery.
Give each of your button elements the classtarget
.
- Add ID Attributes to Bootstrap Elements
You can give each of your elements an
id
attribute.
Eachid
must be unique to a specific element and used only once per page.
Let's give a unique id to each of our div elements of class well.
Remember that you can give an element an id like this:<div class="well" id="center-well">
Give the well on the left the id of left-well. Give the well on the right the id of
right-well
.
- Label Bootstrap Wells
For the sake of clarity, let's label both of our wells with their ids.
Above your left-well, inside itscol-xs-6
div element, add ah4
element with the text#left-well
.
Above your right-well, inside itscol-xs-6
div element, add ah4
element with the text#right-well
.
- Give Each Element a Unique ID
We will also want to be able to use jQuery to target each button by its unique id.
Give each of your buttons a unique id, starting withtarget1
and ending withtarget6
.
Make sure thattarget1
totarget3
are in#left-well
, andtarget4
totarget6
are in#right-well
.
- Label Bootstrap Buttons
Just like we labeled our wells, we want to label our buttons.
Give each of yourbutton
elements text that corresponds to itsid
's selector.
- Learn how Script Tags and Document Ready Work
Now we're ready to learn jQuery, the most popular JavaScript tool of all time. Don't worry about JavaScript itself - we will cover it soon.
Before we can start using jQuery, we need to add some things to our HTML.
First, add ascript
element at the top of your page. Be sure to close it on the following line.
Your browser will run any JavaScript inside ascript
element, including jQuery.
Inside your script element, add this code:$(document).ready(function() { });
We'll learn more about
functions
later. The important thing to know is that code you put inside thisfunction
will run as soon as your browser has loaded your page.
This is important because without your document ready function, your code may run before your HTML is rendered, which would cause bugs.
- Target HTML Elements with Selectors Using jQuery
Now we have a
document ready function
.
Now let's write our first jQuery statement. All jQuery functions start with a$
, usually referred to as adollar sign operator
, or asbling
.
jQuery often selects an HTML element with aselector
, then does something to that element.
For example, let's make all of yourbutton
elements bounce. Just add this code inside your document ready function:$("button").addClass("animated bounce");
即为如下所示:
<script> $(document).ready(function() { $("button").addClass("animated bounce"); }); </script>
- Target Elements by Class Using jQuery
You see how we made all of your button elements bounce? We selected them with $("button"), then we added some CSS classes to them with .addClass("animated bounce");.
You just used jQuery's.addClass()
function, which allows you to add classes to elements.
First, let's target yourdiv
elements with the classwell
by using the$(".well")
selector.
Note that, just like with CSS declarations, you type a.
before the class's name.
Then use jQuery's.addClass()
function to add the classesanimated
andshake
.<script> $(document).ready(function() { $("button").addClass("animated bounce"); $(".well").addClass("animated shake"); }); </script>
- Three Ways For Targeting Element
Three ways of targeting elements: by type:
$("button")
, by class:$(".btn")
, and by id:$("#target1")
.
- Remove Classes from an element with jQuery
In the same way you can add classes to an element with jQuery's
addClass()
function, you can remove them with jQuery'sremoveClass()
function.
- Change the CSS of an Element Using jQuery
We can also change the CSS of an HTML element directly with jQuery.
jQuery has a function called.css()
that allows you to change the CSS of an element.
Here's how we would change its color to blue:$("#target1").css("color", "blue");
- Disable an Element Using jQuery
You can also change the non-CSS properties of HTML elements with jQuery. For example, you can disable buttons.
When you disable a button, it will become grayed-out and can no longer be clicked.
jQuery has a function called.prop()
that allows you to adjust the properties of elements.
Here's how you would disable all buttons:$("button").prop("disabled", true);
- Change Text Inside an Element Using jQuery
Using jQuery, you can change the text between the start and end tags of an element. You can even change HTML markup.
jQuery has a function called.html()
that lets you add HTML tags and text within an element. Any content previously within the element will be completely replaced with the content you provide using this function.
Here's how you would rewrite and emphasize the text of our heading:$("h3").html("<em>jQuery Playground</em>");
jQuery also has a similar function called
.text()
that only alters text without adding tags. In other words, this function will not evaluate any HTML tags passed to it, but will instead treat it as the text you want to replace the existing content with.
- Remove an Element Using jQuery
Now let's remove an HTML element from your page using jQuery.
jQuery has a function called.remove()
that will remove an HTML element entirely
- Use appendTo to Move Elements with jQuery
Now let's try moving elements from one div to another.
jQuery has a function calledappendTo()
that allows you to select HTML elements and append them to another element.
For example, if we wanted to movetarget4
from our right well to our left well, we would use:$("#target4").appendTo("#left-well");
- Clone an Element Using jQuery
In addition to moving elements, you can also copy them from one place to another.
jQuery has a function calledclone()
that makes a copy of an element.
For example, if we wanted to copy target2 from our left-well to our right-well, we would use:$("#target2").clone().appendTo("#right-well");
Did you notice this involves sticking two jQuery functions together? This is called function chaining
and it's a convenient way to get things done with jQuery.
- Target the Parent of an Element Using jQuery
Every HTML element has a
parent
element from which itinherits
properties.
For example, your jQuery Playgroundh3
element has the parent element of<div class="container-fluid">
, which itself has the parentbody
.
jQuery has a function calledparent()
that allows you to access the parent of whichever element you've selected.
Here's an example of how you would use theparent()
function if you wanted to give the parent element of theleft-well
element a background color of blue:$("#left-well").parent().css("background-color", "blue")
- Target the Children of an Element Using jQuery
Many HTML elements have children which
inherit
their properties from their parent HTML elements.
For example, every HTML element is a child of yourbody
element, and your "jQuery Playground"h3
element is a child of your<div class="container-fluid">
element.
jQuery has a function calledchildren()
that allows you to access the children of whichever element you've selected.
Here's an example of how you would use thechildren()
function to give the children of yourleft-well
element the color of blue:$("#left-well").children().css("color", "blue");
- Target a Specific Child of an Element Using jQuery
You've seen why
id
attributes are so convenient for targeting with jQuery selectors. But you won't always have such neat ids to work with.
Fortunately, jQuery has some other tricks for targeting the right elements.
jQuery uses CSS Selectors to target elements.target:nth-child(n)
css selector allows you to select all the nth elements with the target class or element type.
Here's how you would give the third element in each well the bounce class:$(".target:nth-child(3)").addClass("animated bounce");
- Target Even Numbered(偶数) Elements Using jQuery
You can also target all the even-numbered elements.
Here's how you would target all the odd-numbered(奇数) elements with classtarget
and give them classes:$(".target:odd").addClass("animated shake");
Note that jQuery is zero-indexed, meaning that, counter-intuitively, :odd selects the second element, fourth element, and so on.
- Use jQuery to Modify the Entire Page
jQuery can target the
body
element as well.
Here's how we would make the entirebody
fade out:$("body").addClass("animated fadeOut");
But let's do something more dramatic. Add the classes
animated
andhinge
to your body element.