jQuery starting tutorial - using selectors and events
jQuery provides two ways to select HTML elements:
The first is to use CSS and Xpath selectors to combine to form a string to be sent to jQuery's constructor ( Such as: $("div > ul a"));
The second method is to use several methods of jQuery objects. 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.
First, we need to select the list itself, which has an ID called "orderedlist" The usual JavaScript writing is the document.GetelementByid ("OrderEdlist"). In jQuery, we do this: $ (docume). READY () ");
}); Here a CSS style red in the starterkit is attached to the orderedlist (Translator Keel’s note: refer to core.css in the css directory in the test package, which defines the red style). So, 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 remains unchanged.
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 All have the style "blue" appended.
Now let’s do something a little more complicated, switching styles when the mouse is placed on and away from the li object, but it only takes effect on the last element of the list. $(document).ready(function() {
$(document). $(this).removeClass("green");
});
}); There are also tons of similar CSS and XPath examples. More 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 in this paragraph sooner or later!)
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 were changed to XXX and on was removed). Some other events, such as ready and hover, also provide corresponding methods.
You can find the full event list in Visual jQuery, under the Events column.
You can already do a lot of things with these selectors and events, but here is something even better! $(document).ready(function() {
+ " BAM! " + i );
});
});
find() allows you to perform a conditional search in the selected element, so $("#orderedlist).find("li") is Like $("#orderedlist li").
each() iterates over all li, and can do more processing on this basis. Most methods, such as addClass(), can use their own. each().
Another often encountered task is to call some methods on DOM elements that are not covered by jQuery. Imagine a reset after you successfully submit it using AJAX: $(document).ready(function() {
/ / use this to reset a single form
Keel's note: The author here also writes the id of the form as form. The source file has