Introduction to the use of jquery

Source: Internet
Author: User
Tags jquery library jquery addclass

I,

1. What is jquery? What are the features?

Jquery is a JavaScript function library.

The jquery Library has the following features:

HTML element selection, HTML element operations, CSS operations, HTML event functions, JavaScript special effects and animations, HTML Dom traversal and modification, Ajax, utilities

 

II,

2. jquery-related syntax:

(1) With jquery, you can hide and display the effects:

Jquery hide () and show ()

With jquery, you can use the hide () and show () Methods to hide and display HTML elements:

Syntax:

$(selector).hide(speed,callback);$(selector).show(speed,callback);

The optional speed parameter specifies the hidden/display speed. The value can be "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after it is hidden or displayed.

 

Jquery toggle ()

With jquery, you can use the toggle () method to switch between the hide () and show () methods.

Show Hidden elements and hide the displayed elements:

Syntax:

$(selector).toggle(speed,callback);

The optional speed parameter specifies the hidden/display speed. The value can be "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after the toggle () method is complete.

 

(2) jquery allows you to fade in and out elements.

Jquery fadein () shows hidden elements.

Syntax:

$(selector).fadeIn(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after fading is complete.

 

The jquery fadeout () demo method is used to fade out visible elements.

Syntax:

$(selector).fadeOut(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after fading is complete.

 

Jquery fadetoggle () demonstrates that the jquery fadetoggle () method can be switched between fadein () and fadeout () methods. If the element has faded out, fadetoggle () adds a fade-in effect to the element. If the element is fade in, fadetoggle () adds a fade out effect to the element.

Syntax:

$(selector).fadeToggle(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after fading is complete.

 

Jquery fadeto () demonstrates how the jquery fadeto () method allows gradient to be a given opacity (value between 0 and 1 ).

Syntax:

$(selector).fadeTo(speed,opacity,callback);

The required speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The required opacity parameter in the fadeto () method sets the fade-in and fade-out effect to a given opacity (value between 0 and 1 ).

The optional callback parameter is the name of the function executed after the function is completed.

 

(3) jquery sliding method can make the elements slide up and down.

The jquery slidedown () method is used to slide down an element.

Syntax:

$(selector).slideDown(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after sliding.

 

The jquery slideup () method is used to move an element up.

Syntax:

$(selector).slideUp(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after sliding.

 

The jquery slidetoggle () method can be switched between the slidedown () and slideup () methods.

If the elements slide down, slidetoggle () can slide them up.

If the elements slide up, slidetoggle () can slide down them.

$(selector).slideToggle(speed,callback);

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after sliding.

 

(4) jquery animate () allows you to create custom animations.

The jquery animate () method is used to create custom animations.

Syntax:

$(selector).animate({params},speed,callback);

The required Params parameter defines the CSS attributes of the animation.

The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or millisecond.

The optional callback parameter is the name of the function executed after the animation is completed.

 

(5) jquery stop () method is used to stop an animation or effect before it is completed.

The jquery stop () method is used to stop an animation or effect before they are completed.

The stop () method applies to all jquery effect functions, including sliding, fade-in and fade-out, and custom animations.

Syntax

$(selector).stop(stopAll,goToEnd);

The optional stopall parameter specifies whether to clear the animation queue. The default value is false, that is, only the animation that stops the activity allows any animations that are placed in the queue to be executed backward.

The optional gotoend parameter specifies whether the current animation is completed immediately. The default value is false.

Therefore, by default, stop () clears the current animation specified on the selected element.

3. jquery selector:

The previous article focuses on the use of jquery selectors, so I will not list them one by one.

4. jquery-related events:

Event Function Bind a function
$ (Document). Ready (function) Bind the function to the ready event of the document (when the document is loaded)
$ (Selector). Click (function) Click events that trigger or bind a function to the selected Element
$ (Selector). dblclick (function) Double-click event that triggers or binds a function to the selected Element
$ (Selector). Focus (function) Events that trigger or bind a function to the retrieved focus of the selected Element
$ (Selector). Mouseover (function) A mouse hover event that triggers or binds a function to the selected element.

 

III,

Jquery Dom operations

 

A very important part of jquery is its ability to operate the Dom.

Jquery provides a series of dom-related methods, which makes it easy to access and manipulate elements and attributes.

 

1. Obtain the content-text (), HTML (), and Val ()

Three simple and practical jquery methods for Dom operations:

Text ()-set or return the text content of the selected Element

HTML ()-set or return the content of the selected element (including HTML tags)

Val ()-set or return the value of a form field

2. Get attributes-ATTR ()

The jquery ATTR () method is used to obtain the attribute value.

Set attributes-ATTR ()

The jquery ATTR () method is also used to set/change attribute values.

The following example shows how to change the value of the href attribute in the (SET) link:

Instance

$("button").click(function(){  $("#w3s").attr("href","http://www.w3school.com.cn/jquery");});

 

The ATTR () method also allows you to set multiple attributes at the same time.

The following example shows how to set href and title attributes simultaneously:

Instance

$("button").click(function(){  $("#w3s").attr({    "href" : "http://www.w3school.com.cn/jquery",    "title" : "W3School jQuery Tutorial"  });});

ATTR () callback function

The jquery method ATTR () also provides callback functions. The callback function consists of two parameters: the subscript of the current element in the list of selected elements and the original (old) value. Then return the string you want to use with the new value of the function.

The following example demonstrates the ATTR () method with a callback function:

Instance

$("button").click(function(){  $("#w3s").attr("href", function(i,origValue){    return origValue + "/jquery";  });});

 

3. Using jquery, you can easily add new elements/content.

The jquery append () method inserts content at the end of the selected element.

The jquery prepend () method inserts content at the beginning of the selected element.

The jquery after () method inserts content after the selected element.

The jquery before () method inserts content before the selected element.

4. Using jquery, you can easily Delete existing HTML elements.

The jquery remove () method deletes the selected element and its child element.

The jquery empty () method deletes the child elements of the selected element.

5. Using jquery, you can easily operate CSS elements.

To set the specified CSS attribute, use the following syntax:

css("propertyname","value");

The following example sets the background-color value for all matching elements:

Instance

$("p").css("background-color","yellow");

 

Jquery addclass () method

The following example shows how to add class attributes to different elements.

Instance

$("button").click(function(){  $("h1,h2,p").addClass("blue");  $("div").addClass("important");});
Jquery removeclass () method

The following example shows how to delete the class attribute from different elements.

Instance

$("button").click(function(){  $("h1,h2,p").removeClass("blue");});
Jquery toggleclass () method

The following example shows how to use the jquery toggleclass () method. This method adds or deletes the selected element:

Instance

$("button").click(function(){  $("h1,h2,p").toggleClass("blue");});

 

6. dimensions:

Jquery innerwidth () and innerheight () Methods

The innerwidth () method returns the width (including the padding) of the element ).

The innerheight () method returns the height of the element (including the padding ).

Jquery outerwidth () and outerheight () Methods

The outerwidth () method returns the width of the element (including the padding and border ).

The outerheight () method returns the height of an element, including the padding and border ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.