How to use Trigger () in jquery

Source: Internet
Author: User

Trigger () Trigger event

Triggering events is similar to clicking on the click, MouseOver, KeyDown and other actions of the JS event, simply said is an action, may be asked, that show, hide is not? No

Why use Trigger ()?

For example: You add a click Click event to a button, and the code below is displayed.

var div = $ ("#mybutton"); Your button. Div.click (function () {alert (' You are a pig, ~ let you point, you point? ‘);});

The above code is a button's click event. This time you have a non-demand, is to want to refresh the page when you click on this button. If you do not need trigger () you can write in the following way:

Div.click ();

If you use trigger (), you will write this: the effect is the same as the above sentence, that is, a little longer.

Div.trigger ("click");

Then someone said: You are a pig Ah ~~ above this short point, you also teach me to use the following =,=
Watch me, ~? Keep looking down.

You use the index in the manual. Click to see two results click () and, click (FN), and then search trigger found only one trigger (type, [data]).
And then you know what I mean. -trigger can pass parameters in.
Let's look at the trigger example in the Handbook:

$ ("P"). Click (Function (event, a, B) {//A normal click event, A and B are undefined types//if triggered with the following statement, a points to "Foo" and B points to "bar"}). Trigger ("clic K ", [" foo "," Bar "]);

Do you understand? If you use the click, it does not eat the parameters, with trigger words, you can eat.
Practice it: stick to your page and try it, and remember to load jquery in.

$ (function () {var div = $ ("#mybutton");//Your button. Div.click (function (e,text) {var text = text | | ' You are a pig, ~ let you point, you point? ‘; alert (text); }); Div.trigger ("Click", "Hello, please click button");});

Demo

The following code can display a "Hello World" JQuery Code: $ ("P"). Bind ("MyEvent", Function (event, Message1, Message2) {   

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.