In those years, I was still learning the jquery notes _jquery

Source: Internet
Author: User
The original jquery is a JavaScript write a library, it gives us a lot of commonly used methods, use these methods can be compatible with a variety of browsers, use it to achieve a good animation effect, the following is to look at those years of learning notes
One, $ symbol
1, Selector
You can choose HTML tags, you can choose Id,class, etc.
As follows:
Copy Code code as follows:

<script type= "Text/javascript" >
$(
function () {
$ ("Table.datalist tr:nth-child (Odd)"). AddClass ("one");//odd: Matching odd, even: even, Nth-child: matches the nth child or parity element under its parent element, starting at 1
var obj = $ ("H2 a"); Get the H2 below the A and return an array
for (var i = 0; i < obj.length; i++) {
obj[i].innerhtml = i.ToString ();
}
var obj1 = $ ("#demo");
Obj1.append ("demo_id");
var obj2 = $ (". Class "); Gets an array of
alert (OBJ2);
$ ("Div ul Li:has (a)"). AddClass ("two"); To get a sign in there, add a class= "two"
$ ("Li:lt (2)"). AddClass ("one"); Get the first three Li, add a class= "one", the subscript is starting from 0
$ ("P>span"). AddClass ("two"); Get the Span,parent>child under P
}
)
</script>

2, get the attribute in the tag
Copy Code code as follows:

var stitle = $ ("P>span"). attr ("Title"); Get the property value in the tag
$ ("P>a"). Text (Stitle),//Set value, if the value is obtained with text (), the resulting literal, without HTML

3. Set properties
Copy Code code as follows:

$ ("button:gt (0)"). attr ("Disabled", "disabled"); Get the button after the first button and set their disable property to be unavailable and return undefined without this property
$ ("img"). attr ({src: "test.jpg", alt: "Test Image"});

3.1, delete the attribute
$ ("img"). Removeattr ("src");
4, modify class, if there is (does not exist) to delete (add) a class.
Copy Code code as follows:

$(
function () {
$ ("Div>p"). MouseOver (
function () {
$ (this). Toggleclass ("Hightlight");//Modify Style class
}
);
}

4.1, move out class
$ ("P"). Removeclass ("selected");
4.2, add Class
$ ("P"). AddClass ("selected");
5, the copy of the picture
$ ("Img:eq (0)"). Clone (). Appendto ($ ("P")); Get the first picture
6, event binding, you can bind multiple identical events
Copy Code code as follows:

$ (function () {
$ ("img"). Bind ("click", Function () {//Bind event
Alert ("You dot Ah!") 1 ");
});
$ ("img"). Bind ("click", Function () {//Bind event
Alert ("You dot Ah!") 2 ");
});
})

7. Remove the event
Copy Code code as follows:

$ (function () {
$ ("Input[type=button]"). Click (function () {
$ ("img"). Unbind ("click", eventimg);//unbind to remove the method
Alert ("Move out of the event!") ");
});
})

8. Display and hide of elements
Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ ("#demobtnF"). Click (function () {
$ ("#p_btn"). Show (); Show
});
$ ("#demobtn"). Click (function () {
$ ("Span:first"). Hide ();//Hidden
});
})
</script>

The effect of fading: In Show (), and Hide () method to upload a time parameter, so as to get fade effect
Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ ("#Button1"). Click (function () {
$ ("#imgs"). Show (10000);
});
$ ("#Button2"). Click (function () {
$ ("#imgs"). Hide (5000);
});
})
</script>

Copy Code code as follows:

<input id= "Button1" type= "button" value= "Show"/>
<input id= "Button2" type= "button" value= "Hide"/>


Two, fade the realization method
1, FadeIn () and fadeout (): Using the same method as show (), Hide (), the effect is better
As follows:
Copy Code code as follows:

$ ("#Button3"). Click (function () {
$ ("#imgs"). FadeIn (10000);
});
$ ("#Button4"). Click (function () {
$ ("#imgs"). fadeout (5000);
});

2, slide effect-by Slideup () and Slidedown () method implementation
Copy Code code as follows:

$ ("#Button1"). Click (function () {
$ ("#imgs"). Slideup (10000);
});
$ ("#Button2"). Click (function () {
$ ("#imgs"). Slidedown (5000);
});

Three, jquery function function
1, the browser's detection $.browser, as follows:
Copy Code code 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, Box model $.boxmodel as follows:
Alert ($.boxmodel?) "Standard": "IE");
3, processing JavaScript object $.each (arr,function)//array of each object with function functions
As follows:
Copy Code code as follows:

var Aarr = ["One", "two", "three"];
$.each (Aarr, function (inum, value) {
Alert (Inum + "," + value);
});

$.grep (arr,funtion (value)): Filter
Example:
Copy Code code 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 derived from those years of study notes.
Summarize
There are many functions in the jquery library, and some, especially the Ajax in jquery, are very important to recall the days of learning 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.