In those years, I was still learning jquery study notes.

Source: Internet
Author: User

Jquery is a library written in javascript. It provides many common methods. These methods can be compatible with multiple browsers and can achieve good animation effects, let's take a look at the study notes of those years.
1. $ symbol
1. Selector
You can select html tags, ID, Class, etc.
As follows: Copy codeThe Code is as follows: <script type = "text/javascript">
$ (
Function (){
$ ("Table. datalist tr: nth-child (odd )"). addClass ("one"); // odd: matches the odd number, even: even number, nth-child: matches the nth child or parity element under the parent element, starting from 1
Var obj = $("h2 a"); // obtain a under h2, and an array is returned.
For (var I = 0; I <obj. length; I ++ ){
Obj [I]. innerHTML = I. toString ();
}
Var obj1 = $ ("# demo ");
Obj1.append ("Demo_Id ");
Var obj2 = $ (". Class"); // an array is obtained.
Alert (obj2 );
$ ("Div ul li: has (a)"). addClass ("Two"); // Add a Class = "Two"
$ ("Li: lt (2 )"). addClass ("one"); // obtain the first three li, add a Class = "one", and the subscript starts from 0.
$ ("P> span"). addClass ("Two"); // obtain the span, parent> child under p.
}
)
</Script>

2. Get the marked attributes.Copy codeThe Code is as follows: var sTitle = $ ("p> span"). attr ("Title"); // get the attribute value in the tag
$ ("P> a"). text (sTitle); // set the value. If the value is obtained using text (), it is the obtained text, excluding html

3. Set attributesCopy codeThe Code is as follows: $ ("button: gt (0 )"). attr ("disabled", "disabled"); // obtain the button after the first button, and set their disable attribute to unavailable. If this attribute is not available, undefined is returned.
$ ("Img"). attr ({src: "test.jpg", alt: "Test Image "});

3.1 Delete attributes
$ ("Img"). removeAttr ("src ");
4. Modify the Class. If the Class exists (does not exist), delete (ADD) a Class.Copy codeThe Code is as follows: $ (
Function (){
$ ("Div> p"). mouseover (
Function (){
$ (This). toggleClass ("HightLight"); // modify the Style class
}
);
}

4.1 remove Class
$ ("P"). removeClass ("selected ");
4.2 Add a Class
$ ("P"). addClass ("selected ");
5. Copying Images
$ ("Img: eq (0)"). clone (). appendTo ($ ("p"); // obtain the first image
6. Event binding: You can bind multiple identical events.Copy codeThe Code is as follows: $ (function (){
$ ("Img"). bind ("click", function () {// bind event
Alert ("click it! 1 ");
});
$ ("Img"). bind ("click", function () {// bind event
Alert ("click it! 2 ");
});
})

7. removal eventCopy codeThe Code is as follows: $ (function (){
$ ("Input [type = button]"). click (function (){
$ ("Img"). unbind ("click", eventImg); // unbind to remove the Method
Alert ("removal event! ");
});
})

8. Display and hide ElementsCopy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# DemobtnF"). click (function (){
$ ("# P_btn"). show (); // display
});
$ ("# Demobtn"). click (function (){
$ ("Span: first"). hide (); // hide
});
})
</Script>

Effect of Fade-in and fade-out: Upload a time parameter in the show () and hide () methods to get the effect of Fade-in and fade-out.Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# Button1"). click (function (){
$ ("# Imgs"). show (10000 );
});
$ ("# Button2"). click (function (){
$ ("# Imgs"). hide (5000 );
});
})
</Script>

Copy codeThe Code is as follows: <input id = "Button1" type = "button" value = "Show"/>
<Input id = "Button2" type = "button" value = "hide"/>
<Hr/>

<Hr/>

II. Implementation of Fade-in and fade-out
1. fadeIn () and fadeOut (): The Use method is the same as show () and hide (), and the effect is better.
As follows:Copy codeThe Code is as follows: $ ("# Button3"). click (function (){
$ ("# Imgs"). fadeIn (10000 );
});
$ ("# Button4"). click (function (){
$ ("# Imgs"). fadeOut (5000 );
});

2. Slide effect-implemented by the slideUp () and slideDown () MethodsCopy codeThe Code is as follows: $ ("# Button1"). click (function (){
$ ("# Imgs"). slideUp (10000 );
});
$ ("# Button2"). click (function (){
$ ("# Imgs"). slideDown (5000 );
});

Iii. Jquery Functions
1. browser detection $. browser:Copy codeThe Code is as follows: if ($. browser. msie) return alert ("IE ");
If ($. browser. safari) return alert ("safari ");
If ($. browser. mozilla) return alert ("mozilla ");
If ($. browser. opera) return alert ("opera ");

2. The box model $. boxModel is as follows:
Alert ($. boxModel? "Standard": "IE ");
3. process the javascript Object $. each (arr, function) // use the function to process each object in the array.
As follows:Copy codeThe Code is as follows: var partition r = ["one", "two", "three"];
$. Each (random R, function (iNum, value ){
Alert (iNum + "," + value );
});

$. Grep (Arr, funtion (value): Filter
Example:Copy codeThe Code is as follows: var aArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var DemoArr = $. grep (aArray, function (value ){
Return value> 4;
});
Document. write (DemoArr. join ());
$. Map (Array, function (value, index ))

This article is all from the study notes of those years.
Summary
There are many functions in the Jquery library, and some others, especially Ajax in Jquery, are very important. This article will recall the days when I learned Jquery in those years.

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.