1. Overview of jQuery
1. Js library
Library is a specific set (functions, methods) that is wrapped
2. The concept of jQuery is a JS library
Advantages: lightweight/compatibility, etc
2. Basic use of jQuery
1,2 download
<head>
<script src="jquery.js"></script>
</head>
Copy the code
3. JQuery entry functions
$(function() {
$('div').hide();
})
Copy the code
4. JQuery’s top-level object $
A window equivalent to a native JS can call a method through $
5. JQuery object and DOM object
- DOM object native JS object/jQuery object
$(' ')
; To obtain - JQuery object is an object generated by wrapping DOM object with $(stored in pseudo-array form).
JQuery objects can only use jQuery methods, whereas DOM objects use native JS properties and methods
Conversion between DOM objects and jQuery objects
- DOM object to jQuery object:
$(DOM object)
- JQuery object to DOM object:
$('div')[index]
/$('div').get(index)
Commonly used jQuery API
JQuery selector
-
$(‘ selector ‘)
-
Hierarchical selectors: child selectors > etc
-
JQuery implicit iteration (important) —- when performing the same operation on an element
The process of iterating through internal DOM elements (stored as pseudo-arrays) is called implicit iteration
-
JQuery filters the selector
$('li:first')
Gets the first Li element$('li:last')
Gets the last Li element$('li:eq(index)')
Select element index numbers starting at 0 from the obtained li element$('li:odd')
The element whose index number is odd in the obtained li element$('li:even')
The element whose index number is even in the obtained li element
- JQuery Filter method (key)
$('li').parent()
Finding the parent returns the parent of the element at the most recent level$('ul').children('li')
The equivalent of$('ul>li')
The most recent first-degree son$('ul').find('li')
The equivalent of$('ul li')
Descendant selector$('.first').siblings('li')
Find sibling nodes, not including itself$('.first').nextAll()
Find all the peers after the current element$('.first').prevtAll()
Finds all the peers of the current element$('div').hasClass('protected')
Checks if the current element contains a specific class and returns true if so$('li').eq(index)
The equivalent of$('li':eq(index))
Index starts at 0$(this) jQuery
The current element this is not quoted
-
Exclusivity in jQuery
Apply implicit iteration
$(this).index() $(this).index()
2. JQuery style manipulation
1. Operate the CSS method
$(this).css('color')
// Write only the attribute name, return the value of the attribute$(this).css('color','red')
$(this).css({ ... })
// If the object is a compound attribute, it must be humped. If the value is not a number, it must be quoted
2. Set the class style method
- Add the class:
$('div').addClass('current')
- Delete categories:
$('div').removeClass('current')
- Switch:
$('div').toggleClass('current')
Chain programming:
Reason: Since many jQuery methods return the current jQuery object directly, you can continue. The method;
- Val (), HTML (),text(),attr(), return a text string;
- Next () nextAll() parent() children() prev()…
- Most of the rest returns the current jQuery object;
3. Class operations are different from classNames
ClassName overrides the original className in jQuery, but addClass does not
JQuery effects
1. Show hide
- show([speed],[easing],[fn])
- hide([speed],[easing],[fn])
- Switch: toggle (); Under normal circumstances, there is no parameter directly hidden display
Slide 2.
- Sliding slideDown ();
- Sliding slideUp ()
- Toggle slideToggle()
3. Event toggle hover([over],out)
- Event switch hover if you write only a function that triggers both mouse over and mouse off
4. Animation queue and its stopping queuing method
- Stop queuing stop() needs to be written at the beginning of the animation
5. Fade in and out
- Fade in fadeIn ();
- Fade out fadeOut ();
- fadeTo(speed,opacity); Opacity must be set to 0~1
- Switch fadeToggle ();
Customize animate
The animate grammar (params, [speed], [much], [fn])
Params: Style properties that you want to change, passed as objects, must be written. Attribute names can be unquoted, and camel name is required for compound attributes.
Animate returns the top: // Instead of a document, HTML and body animate
$("body,html").stop().animate({
scrollTop:0
})
Copy the code
4. JQuery attribute manipulation
1. Set or get the element’s inherent property value prop()
- Get attribute syntax
Prop (" property ")
- Set property syntax
Prop (" property "," property value ")
2. Set or get the element’s custom attribute attr()
- Get attribute syntax
Attr (" property ")
// Similar to native getAttribute() - Set property syntax
Attr (" attribute "," attribute value ")
// Similar to native setAttribute() - Custom attributes for H5 are also available
(data-...)
Data (), where the data is stored in the element’s memory
- Add-on data syntax
data("name","value")
// Appends data to the selected element - Fetch data syntax
date("name")
// Get data from the selected element - You can get H5 custom properties
data-index
Get a number
$(“.j-checkbox:checked”).length
5. Text value of jQuery content
- The normal element content HTML () is equivalent to the native innerHTML
- The plain element text content text() is equivalent to the native innerText
- Gets the Settings form value val() equivalent to the native value
Other:
- Intercept the string substr(1)
- parents(); The selector finds all ancestor elements
- Praents (” selector “); The selector finds the specified ancestor element
- Keep two decimal places. ToFiexd (2);
JQuery element manipulation
1. Iterate over elements
- Grammar 1:
$("div").each(function(index,demEle){})
Index is the index number, demEle is each DOM element object, not jQuery object (need to convert to jQuery object $(demEle)) - Syntax 2:
$.each($("div"),funciton(index,demEle{}))
The method iterates over elements primarily for iterating over data (arrays/objects)
Processing data $. Each (arr / {name: “”, the age:}, funcition (I, ele) {})
2. Create elements
$("<li></li>");
3. Add elements
- Internal additions: element.append(); // Put it at the end of the content
Element.prepend (); // put it at the top of the content
- External add: element.after(” content “) // Place the content after the element
Before (” content “) // Put the content before the element
- Delete element:
- element.remove(); // Delete the matched element (itself)
- element.empty(); // Remove all child nodes in the set of matched elements
- Element.html (“”””) // Empties the content of the matched element
JQuery event
JQuery event registration
Element. Event (function(){})
JQuery event handling
1. Event handling on() binding event
Syntax: element.on(events,[selector: child selector of an element],fn)
Event delegate: Binds events originally assigned to child elements to parent elements
3. Automatically trigger event trigger()
- Element. Event ()
- Element. Trigger (” event “)
- TriggerHandler (” event “) does not trigger the element’s default behavior
JQuery event object
- Prevent default behavior:
event.preventDefault()
orreturn false
- To prevent bubbling:
event.stopPropagation()
JQuery Other methods
$.extend([deep],target,object1,[objectN])
- Deep :true Indicates deep copy. The default value is false. Shallow copy
- Target: indicates the object to copy
- Object1: Object to be copied to the first object
- Shallow copy is to copy the address of a complex data type to the target object. Modifying the target object will affect the copied object
- Deep copy full clone, all data objects, will not be overwritten, if there are no conflicting attributes will be merged together
2. Coexistence of multiple libraries
Solution:
- Change the $sign to jquery
- The jQuery variable specifies a new name:
$.noConflict()
var xx=$.noConflict();
JQuery plugin: The home of jQuery
- Waterfall flow plug-in
- Image lazy loading: The visual area shows the image JS written at the end of the DOM element
- Full screen slide
- The bootstrap plug-in
JQuery size, position operation
JQuery dimensions
- Width ()/height() //
- InnerWidth ()/innerHeight() // contains the padding
- OuterWidth ()/outerHeight() // includes padding, border
- OuterWidth (true)/outerHeight(true) // Contains padding, border, and margin
Location of jQuery
offset() position() scrollTop()/Left()
- 1. Offset () Gets the setting position (offset) from the document independent of the parent
- 2. Position () gets the position(offset) relative to the positioned parent element. If there is no positioned parent, base it on the document
- 3. ScrollTop ()/scrollLeft() gets the head and left side of the setting to be rolled out