Home > Web Front-end > JS Tutorial > jQuery event delegation instance analysis_jquery

jQuery event delegation instance analysis_jquery

WBOY
Release: 2016-05-16 15:50:14
Original
1484 people have browsed it

Event delegation is mainly implemented by utilizing the event bubbling phenomenon. Accurate mastery of event delegation can help improve code execution efficiency. Let’s look at a code example first:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>脚本之家</title>
<style type="text/css">
table{
 width:300px;
 height:60px;
 background-color:green;
} 
table td{
 background-color:white;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $("td").bind("click",function(){
  $(this).text("哈哈");
 })
})
</script>
</head>
<body>
<table cellspacing="1">
 <tr>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
 </tr>
 <tr>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
 </tr>
</table>
</body>
</html>

Copy after login

In the above code, the bind() method is used to bind a click event handler to each td, so that when the cell is clicked, the text in the cell will be reset. Although this method achieves the desired effect and looks very perfect, it is not the case. If there are too many cells, traversing the cells and binding event processing functions to each cell will greatly reduce the performance of the code. If you want the parent element of the cell to listen to the event, you only need to determine whether the DOM element that initially triggered the event is td.

The code is modified as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>脚本之家</title>
<style type="text/css">
table{
 width:300px;
 height:60px;
 background-color:green;
} 
table td{
 background-color:white;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $("table").bind("click",function(e){
  var target = e.target;
  $target=$(target);
  if ($target.is("td")){
   $target.text('哈哈');
  }
 })
})
</script>
</head>
<body>
<table cellspacing="1">
 <tr>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
 </tr>
 <tr>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
  <td>单元格</td>
 </tr>
</table>
</body>
</html>

Copy after login

The above code implements the same function, but the efficiency is greatly improved.

Summary: The so-called event delegation means that the event target does not process the event itself, but delegates the processing task to its parent element or ancestor element, or even the root element.

The above is the entire content of this article, I hope you all like it.

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