Home > Article > Web Front-end > jQuery implements methods to automatically call and trigger an event
The example in this article describes how jQuery can automatically call and trigger an event. Share it with everyone for your reference, the details are as follows:
I will take the click event as an example to study this topic:
jQuery automatically triggers the click event
1. For example, if we define a click event through jquery, how can we automatically trigger it? :
$(function(){ $('#button').click(function(){ alert('button is clicking!'); }); })
1) Automatically trigger click events
$('#button').click();
This was beyond my expectation. I thought this would overwrite the previous encapsulation event
In fact, it didn’t. Instead, the anonymous function inside is called,
This way we can easily understand why some functions have both the function of assignment and value acquisition
$('input').val('默认值'); var templete = $('input').val();
In fact, the principle is to put it plainly, it is to directly return the parameters inside.
2) Here is another trigger method:
$('#button').trigger('click');