Home > Web Front-end > JS Tutorial > Analyze the role and usage of the on() function in jQuery

Analyze the role and usage of the on() function in jQuery

巴扎黑
Release: 2017-06-25 10:39:18
Original
1821 people have browsed it

This article mainly introduces the usage of the on() method in jQuery. The example analyzes the function and definition of the on() method and the use of binding one or more event processing functions on matching elements. For tips, friends who need them can refer to

This article explains the usage of the on() method in jQuery with examples. Share it with everyone for your reference. The specific analysis is as follows:

This method can bind one or more event handling functions to the matching element.
Use the off() method to delete the event bound by the on() method.

Grammar structure one:

The code is as follows:

$(selector).on(events,[selector], [data],fn)

Parameter list:

Parameters Description
events One or more space-separated event types and an optional namespace.
selector Optional. A selector String used to filter the selected elements, the handler will be called on descendant elements of this selector.
If the selection is empty or ignored, the event is always fired when it reaches the selected element.
data Optional. An additional data object passed to the event object as the value of the event.data property for processing by the event handler function.
fn Function executed when this event is triggered. The false value can also be used as a shorthand for a function that returns false.

Example code:

Example 1:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<style type="text/css">
p{
  width:60px;
  height:60px;
  border:1px solid green;
  font-size:12px;
}
</style>
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("p").on("click",function(){ 
    $(this).text("脚本之家欢迎您"); 
  }) 
}) 
</script>
</head>
<body>
<p>原来内容</p>
</body>
</html>
Copy after login

The above code binds a click event to p. When p is clicked Ability to set new text content for p.

Example 2:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<style type="text/css">
p{
  width:60px;
  height:60px;
  border:1px solid green;
  font-size:12px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("p").on("click mousemove",function(){ 
    $(this).text("脚本之家欢迎您"); 
  }) 
}) 
</script>
</head>
<body>
<p>原来内容</p>
</body>
</html>
Copy after login

The above code binds two events to p. Whether you click p or move the mouse in p, a new event will be set for p. text content.

Example 3:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title> 
<style type="text/css"> 
p{ 
  width:60px; 
  height:60px; 
  border:1px solid green; 
  font-size:12px; 
} 
</style> 
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
  var newtext="这是新文本" 
  $("p").on("click",{"mytext":newtext},function(e){ 
    $(this).text(e.data.mytext); 
  }) 
}) 
</script> 
</head> 
<body> 
  <p>原来内容</p> 
</body> 
</html>
Copy after login

The above code uses the data parameter to pass data to the bound event processing function.

Example 4:

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<style type="text/css">
.parent{
  width:160px;
  height:160px;
  border:1px solid green;
  font-size:12px;
}
.children{
  width:100px;
  height:100px;
  border:1px solid red;
}
span{background-color:green;}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  var newtext="这是新文本" 
  $(".parent").on("click",".children",{"mytext":newtext},function(e){ 
    $(this).text(e.data.mytext); 
  }) 
}) 
</script>
</head>
<body>
<p class="parent">
  <p class="children"><span>原来内容</span></p>
</p>
<span>大家好</span>
</body>
</html>
Copy after login

The above code uses the selector string to filter which of the child elements of the matching element can respond to the bound event. In the above code, the element with the class name children and its child elements can call bound events.
Grammar structure two:

The code is as follows:

$(selector).on(object,[selector],[data])

parameter list:

参数描述
object一个用字符串表示的,一个或多个空格分隔的事件类型和可选的命名空间,值表示事件绑定的处理函数。
selector可选。一个选择器字符串,用以过滤选定的元素,该选择器的后裔元素将调用处理程序。
如果选择是空或被忽略,当它到达选定的元素,事件总是触发。
data可选。作为event.data属性值传递给事件对象的额外数据对象以供事件处理函数处理。

实例代码:

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" /> 
<title>脚本之家</title> 
<style type="text/css"> 
p{ 
  width:160px; 
  height:160px; 
  border:1px solid green; 
  font-size:12px; 
} 
</style> 
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
  var newtext="这是新文本" 
  $("p").on({click:function(e){ 
    $(this).text(e.data.mytext); 
  }},{"mytext":newtext}) 
}) 
</script> 
</head> 
<body> 
  <p>原来内容</p> 
</body> 
</html>
Copy after login

以上代码中,事件类型和事件处理函数以对象的形式绑定的。

The above is the detailed content of Analyze the role and usage of the on() function in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template