Home >Web Front-end >JS Tutorial >The simplest jQuery program for beginners to learn_jquery

The simplest jQuery program for beginners to learn_jquery

WBOY
WBOYOriginal
2016-05-16 18:50:301039browse
Copy code The code is as follows:



< ;STYLE type='text/css'>
.css1{
display:block;
width:100px;
height:100px;
background-color:blue;
}
.css2{
display:block;
width:100px;
height:100px;
background-color:red;
}



tt

<script> <br>$(document).ready(function(){ <br>$("#freee").hover( function(){ <br>$(this).addClass("css2"); <br>}, function(){ <br>$(this).removeClass("css2"); <br>}); <br>}); <br></script>




Find me: using selectors and events
jQuery provides two ways to select elements of html. The first is to use CSS and Xpath selectors to combine to form a string and send it to jQuery's constructor (such as: $("div > ul a")) ;The second method is to use several methods of the jQuery object. These two methods can also be used in combination.

To test these selectors, let’s try to select and modify the first ordered list in our starterkit.html.

Initially, we need to select the list itself, this list There is an ID called "orderedlist", and the usual javascript writing method is document.getElementById("orderedlist"). In jQuery, we do this:

$(document).ready(function() {
$("#orderedlist").addClass("red");
}); Here a CSS style red in the starterkit is attached to the orderedlist (Translator Keel's Note: Refer to the core in the css directory in the test package .css, in which the red style is defined). Therefore, after you refresh the starterkit.html, you will see that the background color of the first ordered list changes to red, while the second ordered list does not change.

Now , let's add some new styles to the child nodes of the list.

$(document).ready(function() {
$("#orderedlist > li").addClass("blue" );
}); In this way, all li in the orderedlist are appended with the style "blue".

Now let’s do something a little more complicated. When the mouse is placed on the li object and moved away, the style will be switched, but it will only take effect on the last element of the list.

$(document).ready(function() {
$("#orderedlist li:last").hover(function() {
$(this).addClass("green" );
}, function() {
$(this).removeClass("green");
});
}); There are also a large number of similar CSS and XPath examples, more Many examples and lists can be found here. (Translator Keel’s note: Read this article to get started. Cultivation depends on the individual. If you want to know more after getting started, you must read the links of this paragraph sooner or later! You won’t have to translate it again...^_ ^!)

Every onXXX event is valid, such as onclick, onchange, onsubmit, etc., all have jQuery equivalent representation methods (Translator Keel's note: jQuery doesn't like onXXX, so they are changed to XXX and removed on).
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn