- Create a Bootstrap Headline
To start with, create an
h3element, with the text jQuery Playground.
Color yourh3element with thetext-primaryBootstrap class, and center it with thetext-centerBootstrap 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 yourh3element within adivelement with the classcontainer-fluid.
- Create a BootStrap Row
Now we'll create a Bootstrap row for our inline elements.
Create adivelement below theh3tag, 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 twodivelements within yourrow, both with the classcol-xs-6.
- Create Bootstrap Wells
Bootstrap has a class called
wellthat can create a visual sense of depth for your columns.
Nest onedivelement with the classwellwithin each of yourcol-xs-6div elements.
- Add Elements within your Bootstrap Wells
Now we're several
divelements deep on each column of our row. This is as deep as we'll need to go. Now we can add ourbuttonelements.
Nest threebuttonelements within each of yourwelldiv elements.
- Apply the Default Bootstrap Button Style
Bootstrap has another button class called
btn-default.
Apply both thebtnandbtn-defaultclasses to each of yourbuttonelements.
- 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
idattribute.
Eachidmust 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-6div element, add ah4element with the text#left-well.
Above your right-well, inside itscol-xs-6div element, add ah4element 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 withtarget1and ending withtarget6.
Make sure thattarget1totarget3are in#left-well, andtarget4totarget6are in#right-well.
- Label Bootstrap Buttons
Just like we labeled our wells, we want to label our buttons.
Give each of yourbuttonelements 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 ascriptelement at the top of your page. Be sure to close it on the following line.
Your browser will run any JavaScript inside ascriptelement, including jQuery.
Inside your script element, add this code:$(document).ready(function() { });We'll learn more about
functionslater. The important thing to know is that code you put inside thisfunctionwill 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 yourbuttonelements 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 yourdivelements with the classwellby 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 classesanimatedandshake.<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 movetarget4from 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
parentelement from which itinheritsproperties.
For example, your jQuery Playgroundh3element 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-wellelement 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
inherittheir properties from their parent HTML elements.
For example, every HTML element is a child of yourbodyelement, and your "jQuery Playground"h3element 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-wellelement the color of blue:$("#left-well").children().css("color", "blue");
- Target a Specific Child of an Element Using jQuery
You've seen why
idattributes 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 classtargetand 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
bodyelement as well.
Here's how we would make the entirebodyfade out:$("body").addClass("animated fadeOut");But let's do something more dramatic. Add the classes
animatedandhingeto your body element.









网友评论