Home  >  Article  >  Web Front-end  >  JQuery simulates click events and automatically triggers events

JQuery simulates click events and automatically triggers events

小云云
小云云Original
2017-11-17 11:51:1638842browse

Sometimes, it is necessary to simulate user operations to achieve the click effect. For example, after the user enters the page, the click event is triggered without the user having to actively click. In JQuery, simulation operations can be completed using the trigger() method. For example, you can use the following code to trigger the click event of the button with id btn. In this article, we will explain to you how JQuery simulates click events and automatically triggers events.

 $('#btn').trigger("click");

In this way, when the page is loaded, the desired effect will be output immediately. You can also directly abbreviate click() to achieve the same effect:

$('#btn').click();

Trigger custom events

trigger() method can not only trigger events with the same name supported by the browser, but also Trigger an event with a custom name. For example, to bind a "myClick" event to an element, the JQuery code is as follows:

  $('#btn').bind("myClick", function(){      
 $(&#39;#test&#39;).append("<p>我的自定义事件.</p>");      
 });

If you want to trigger this event, you can use the following code to achieve it:

$(&#39;#btn&#39;).trigger("myClick");

Pass data

## The #trigger(type[,data]) method has two parameters. The first parameter is the event type to be triggered, and the second parameter is the additional data to be passed to the event processing function, passed in the form of an array. You can usually distinguish whether this event is triggered by code or user by passing a parameter to the callback function.

The following is an example of passing data.

$(function(){      
$(&#39;#btn&#39;).bind("myClick", function(event, message1, message2){      
$(&#39;#test&#39;).append( "<p>"+message1 + message2 +"</p>");      
 });      
 $(&#39;#btn&#39;).click(function(){      
 $(this).trigger("myClick",["我的自定义","事件"]);      
}).trigger("myClick",["我的自定义","事件"]);      
 })

Perform the default operation

After the trigger() method triggers the event, the browser default operation will be performed. For example:

 $("input").trigger("focus");

The above code will not only trigger the focus event bound to the d5fd7aea971a85678ba271703566ebfd element, but also cause the d5fd7aea971a85678ba271703566ebfd element itself to get focus (this is the browser's default operation).

If you only want to trigger the bound focus event without performing the browser's default operation, you can use another similar method in jQuery - the triggerHandler() method.

$("input").triggerHandler("focus");

This method will trigger the specific event bound to the d5fd7aea971a85678ba271703566ebfd element, and at the same time cancel the browser's default operation for this event, that is, the text box will only trigger the bound focus event and will not receive focus.

This article mainly introduces JQuery to simulate click events and automatically trigger events. I hope it will be helpful to everyone.

Related recommendations:

js simulate click event

js simulate click event implementation code_javascript skills

Javascript simulates click events (click links and html clicks) compatible with IE/Firefox_javascript skills

The above is the detailed content of JQuery simulates click events and automatically triggers events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn