Home  >  Article  >  Web Front-end  >  Usage examples of trigger() method in jQuery

Usage examples of trigger() method in jQuery

巴扎黑
巴扎黑Original
2017-06-25 13:27:481455browse

This article mainly introduces the usage of the trigger() method in jQuery. The example analyzes the function, definition and usage skills of the trigger() method to trigger the specified type of event of the matching element. Friends in need can refer to it

The example in this article describes the usage of the trigger() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method triggers events of the specified type that match the element.

Grammar structure 1:
Specifies the event type that is triggered by the matching element.

The code is as follows:

$(selector).trigger(event,param1,param2,...)

Parameter list:

eventparam procedure.
Parameter Description
#Specifies the event to be triggered by the specified element. It can be a custom event (attached using the bind() function), or any standard event.
Optional. Additional parameters passed to the event handler The extra parameters are especially useful for custom events.

Example code:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>trigger()函数-脚本之家</title>
<style type="text/css">
p{
  width:200px;
  height:200px;
  border:1px solid blue;
}
</style>
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(
document
).ready(function(){
  $("p").click(function(){
    $("p").append("添加的内容");
  });
  $("button").click(function(){
    $("p").trigger("click");
  })
})
</script>
</head>
<body>
  <p></p>
  <button>点击激活事件</button>
</body>
</html>

When the button button is clicked, the click event on p can be triggered, and then the click event processing function can be executed.

Grammar structure two:

Using the event object as a parameter specifies the event type to be triggered for the matching element.

The code is as follows:

$(selector).trigger(eventObj)

Parameter list:

Parameters Description
eventObj The event object specifies the function to be run when the event occurs.

Example code:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>trigger()函数-脚本之家</title>
<style type="text/css">
p{
  width:200px;
  height:200px;
  border:1px solid blue;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("p").click(function(){
    $("p").append("添加的内容");
  });
  var e=jQuery.Event("click");
  $("button").click(function(){
    $("p").trigger(e);
  })
})
</script>
</head>
<body>
  <p></p>
  <button>点击激活事件</button>
</body>
</html>


The above is the detailed content of Usage examples of trigger() method in jQuery. 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