Common and important jQuery methods, jquery Summary

Source: Internet
Author: User

Common and important jQuery methods, jquery Summary

1. jquery data (name)

The data () method attaches data to the selected element or obtains data from the selected element.

$("#btn1").click(function(){ $("div").data("greeting", "Hello World");});$("#btn2").click(function(){ alert($("div").data("greeting"));});

2. jquery arguments

Arguments is a reference to a real parameter object, and a real parameter object is a class array object.

The arguments index increases from 0,..., and corresponds to the real parameters one by one.

The arguments. length attribute indicates the number of real parameters.

Arguments must not be an array. It is a long object like an array, although it also has the length attribute.

Each function has arguments. Therefore, arguemets only finds its own arguments internally and cannot reference the arguments of the outer layer.

// Calculate the circular area, rectangular area, and triangular area function area () {if (arguments. length = 1) {alert (3.14 * arguments [0] * arguments [0]);} else if (arguments. length = 2) {alert (arguments [0] * arguments [1]);} else if (arguments. length = 3) {alert (arguments [0] + arguments [1] + arguments [2]);} else {return null ;}// call area, 30 );

3. jquery target () event.tar get

The target attribute specifies which DOM element triggers the event.

$ ("P, button, h1, h2 "). click (function (event) {$ ("div" example .html ("Triggered by a" + event.tar get. nodeName + "element. ");}); <p> </p> <button> </button> 

4. jquery trigger (event, [parameter 1, parameter 2,...])

Trigger () method triggers the specified event type of the selected element. (Custom events and parameters can be passed) custom events are very important and useful!

// MyEvent is the custom event name $ ("# p1 "). bind ("myEvent", function (str1, str2) {alert (str1 + ''+ str2) ;}); $ (" # p1 "). trigger ("myEvent", ["Hello", "World"]); // You can also write as follows: $ ("# p1 "). bind ("myEvent", function (str1, str2) {alert (str1 + ''+ str2 );}). trigger ("myEvent", ["Hello", "World"]);

5. js substring (start, stop)

The substring () method is used to extract characters of a string between two specified subscripts.

The substring returned by the substring () method includes the characters at start, but not the characters at stop.

Var str = "Helloworld! "Document. write (str. substring (3, 7) // The result is lowovar str =" Hello world! "// There are two empty characters: document. write (str. substring () // result lo // The results of the two are different. The null strings between strings occupy the index!

It is clear that there is no character at the stop of r!

Important: Unlike slice () and substr (), substring () does not accept negative parameters.

6. js slice (start, stop)

The slice () method can extract a part of a string and return the extracted part with a new string. Like the above substring, it does not include the character at the stop;

Another difference is that start and stop can use negative numbers! That is to say,-1 refers to the last character of the string,-2 refers to the second to last character, and so on.

The data () method attaches data to the selected element or obtains data from the selected element.

The above is all the content of this article. I hope you will like it.

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.