Jquery precautions and common syntaxes _ jquery

Source: Internet
Author: User
Jquery notes and common syntax summary, it is best to add jquery to your favorites. 1. the selector contains special characters.
The selector contains special characters such as ".", "#", "(", "]". According to W3C rules, attribute values cannot contain these special characters. For example:

The Code is as follows:


Bb




For example:

The Code is as follows:


$ ("# Id # B ");
$ ("# Id [1]");


The above Code cannot get the elements correctly. The correct syntax is as follows:

The Code is as follows:


$ ("# Id \ # B ");
$ ("# Id \ [1 \]");


2. About separators containing Spaces
In the following example, the HTML code is as follows:

The Code is as follows:



Aa


Bb


Cc


Dd



Ee


Ff



Use the jquery selector to obtain them:

The Code is as follows:


Alert ($ (". test: hidden"). length); // output 4
Alert ($ (". test: hidden"). length); // output 3


Different results are generated because the descendant selector is different from the filter selector.

The Code is as follows:


$ (". Test: hidden"). length; // with spaces


The above code selects the hidden element in the element whose class is "test.

The Code is as follows:


$ (". Test: hidden"). length; // without spaces


The above code selects the hidden class as the "test" element.
3. val () method
In jquery, the val () method is to read from the last option. If any of the values or text of the option matches, it is selected. For example:

The Code is as follows:


2
1


Whether val ("1") or val ("2") is used, the option
4. css () method
If the parameter value is a number, it is automatically converted to a pixel value. If the attribute contains the "-" symbol, such as font-size and background-color, if the values of these attributes are not enclosed in quotation marks, you must use the camper method. For example:

The Code is as follows:


$ ("P" ).css ({fontSize: "30px", backgroundColor: "#888888 "});


If quotes are enclosed, they can be written as "font-size" or "fontSize". It is recommended that you add quotation marks to form a good habit.
5. About the height () method
(1) The height () method after jQuery1.2 can be used to obtain the height of the window and document.
(2) The difference between using the css () method to obtain the height value and the height () method is that the height value obtained by the css () method is related to the style setting, you may get "auto" or a string like "10px". The height obtained by the height () method is the actual height of the element on the page, it has nothing to do with style settings, and does not contain units.
6. Attributes of event
JQuery encapsulates the common attributes of event objects so that event processing can run properly in all major browsers without browser type judgment.
(1) event. type () method
This method is used to obtain the event type.

The Code is as follows:


$ ("A"). click (function (event ){
Alert (event. type); // obtain the event type
Return false; // block link jump
});


After the preceding code is run, "click" is returned ".
(2) event. preventDefault () method
This method is used to prevent default event behavior. The W3C-compliant preventDefault () method in javascript is invalid in IE browser. jQuery encapsulates it to make it compatible with various browsers.
(3) event. stopPropagation () method
This method is used to prevent event bubbles. The stopPropagation () method conforming to W3C standards in javascript is invalid in IE browser. jQuery encapsulates it so that it is compatible with various browsers.
(4)、event.tar get () method
This method is used to obtain the elements that trigger the event. JQuery's encapsulation avoids the differences between different browsers.

The Code is as follows:


$ ("A [href = http://baidu.com]"). click (function (event ){
Alert(event.tar get. href); // gets the href attribute value of the element that triggers the event.
Return false;
});


After the above Code is run, return "http://baidu.com ".
(5) event. relatedTarget () method
In standard dom, elements generated by mouseoverand mouseoutcan be accessed through the event.tar get () method, and relevant elements are accessed through the event. relatedTarget () method. Event. the relatedTarget () method is equivalent to the event of IE browser in mouseover. the fromElement () method is equivalent to the event of IE browser in mouseout. toElement () method. jQuery encapsulates it to make it compatible with various browsers.
(6) event. pageX () method/event. pageY () method
This method is used to obtain the x and y coordinates of the cursor relative to the page. If jquery is not used, event is used in IE browser. x ()/event. the y () method is used in the firefox browser. pageX ()/event. pageY () method. If a scroll bar exists on the page, the height and width of the scroll bar are also added. In ie, the default 2px border should also be subtracted.

The Code is as follows:


$ ("A"). click (function (event ){
Alert ("Current mouse position:" + event. pageX + "," + event. pageY); // obtain the coordinates of the Current mouse relative to the page
Return false;
});


(7), event. which () method
The function of this method is to get the left, center, and right mouse buttons from the mouse click event; to get the keyboard buttons from the keyboard event.

The Code is as follows:


$ (Function (){
$ ("Body"). mousedown (function (e ){
Alert (e. which); // 1 = left mouse button; 2 = middle mouse button; 3 = right mouse button
});
});


(8), event. metaKey () method
For different browsers Different key-press interpretations, jquery is also encapsulated and requires that the event. metaKey () method is obtained from a keyboard event Press the key.
(9), event. originalEvent () method
This method is used to point to the original event object.
7. bind () method
(1) bind multiple event types

The Code is as follows:


$ (Function (){
$ ("P"). bind ("mouseover mouseout", function (){
$ (This). toggleClass ("over ");
});
});


(2) Add an event namespace

The Code is as follows:


$ (Function (){
$ ("P"). bind ("click. plugin", function (){
$ ("Body"). append ("

Click

");
});
$ ("P"). bind ("mouseover. plugin", function (){
$ ("Body"). append ("

Mouseover

");
});
$ ("P"). bind ("dblclick", function (){
$ ("Body"). append ("

Dblclick

");
});
$ ("Button"). click (function (){
$ ("P"). unbind (". plugin ");
});
});


Add a namespace after the Bound event type so that you only need to specify the namespace When deleting the event. Click After the element, the namespace of "plugin" is deleted, but the "dblclick" event in the namespace of "plugin" still exists.
(3) same event name and different namespace execution methods

The Code is as follows:


$ (Function (){
$ ("P"). bind ("click", function (){
$ ("Body"). append ("

Click

");
});
$ ("P"). bind ("click. plugin", function (){
$ ("Body"). append ("

Click. plugin

");
});
$ ("Button"). click (function (){
$ ("P"). trigger ("click! "); // Note the exclamation point after clicking
});
});


When you click

The click event and click. plugin event are triggered at the same time. If you only click

Only the click event is triggered, but the click. plugin event is not triggered. Note: trigger ("click! ") The exclamation point is used to match all the click methods that are not included in the namespace. To trigger both of them, you only need to remove the exclamation point.
8. About animations in jQuery
(1) jQuery is required for animation effects in standard mode. Otherwise, animation jitter may occur. The standard mode requires that the file header contain the following DTD definition:

(2) Any animation effect in jQuery can be specified with three speed parameters, that is, "slow", "normal", and "fast" (the time length is 0.6 seconds, 0.4 seconds, and 0.2 seconds respectively ). Quotation marks are required when the speed keyword is used, for example, show ("slow"). If a number is used as a time parameter, no quotation marks are required, for example, show (1000 ).
(3) In order to affect the "top", "left", "buttom", and "right" style attributes of an element, you must first set the position style of the element to "relative" or "absolute ".
9. About the load () method
This method is usually used to obtain static data files from the web server.
(1) Use load () to filter the loaded HTML document content
The syntax of the URL parameter of the load () method is "url selector". Note that there is a space between the url and selector. For example, you can use $ ("# xxx"). load ("test.html. para") to parse the content of "para" in the test.html page ");
(2) callback function parameters of the load () method
The load () method provides three callback functions, which represent the content returned by the request, the Request status, and the XMLHttpRequest object. The Code is as follows:

The Code is as follows:


# ("# ResText"). load ("test.html", function (responseText, textStatus, XMLHttpRequest ){
// ResponseText: Content returned by the request
// TextStatus: Request status: success, error, notmodified, and timeout
// XMLHttpRequest: XMLHttpRequest object
});


Note: In the load () method, no matter whether the Ajax request is successful or not, the callback function is triggered after the request is complete. This corresponds to the complete callback function in the $. ajax () method.
10. About the $. get () method
(1) The callback function of this method is called only when the return status of response is success.
(2) The callback function of this method has two parameters, namely, data (which can be an XML file, a JSON file, or an HTML segment), textStatus is the Request status (success, error, notmodified, timeout)
Note that the $. post () method is basically the same as the $. get () method.
11. Content Selector
Content selector: The contains () separator is case sensitive.
12. General Plug-ins

The Code is as follows:


(Function ($ ){
$. Fn. extend ({
"Resize": function (userOptions ){
Var defaultOptions = {height: 100, width: 100 };
Var mergeOptions = $. extend ({}, defaultOptions, userOptions );
Return this. each (function (){
$ (This). animate (mergeOptions, "slow", function (){
$ (This). fadeTo ("slow", ". 70 ")
});
});
}
});
}) (JQuery );


Note:
1. this in Plug-in functions is generally only a jQuery object, for example, this in the sixth row. However, this in this. each indicates a DOM object.
Related Article

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.