jQuery事件性能:在每个孩子的父事件或个人事件上绑定一个事件?

Ada*_*dam 3 javascript jquery javascript-events

我有一个div有大量的儿童div(大约4k-5k!).每个孩子都有一个'mousedown''mouseup'连接到它的事件.

我是否应该将这些事件一次性附加到父母并选择孩子使用$(e.target)?是否会有任何性能优势,如果是这样,为什么?

谢谢!

Eri*_*ips 6

我想可以使用jquery.on()是理想的.

$("#divWithHugeAmountsOfChildDiv").on("mousedown", 
                                      ".childCLASSEStoMouseDown", 
                                      function()
{
  alert("Booya!");
});
Run Code Online (Sandbox Code Playgroud)

你的html可能是这样的:

<body>
  <!-- lots of other elements -->
  <div id="divWithHugeAmountsOfChildDiv">
    <!-- lots of different type of elements -->
    <div class="childCLASSEStoMouseDown">Booya?</div>
    <!-- lots of different type of elements -->
  </div>
  <!-- lots of other elements -->
</body>
Run Code Online (Sandbox Code Playgroud)

根据需要更改jQuery选择器...

为什么在该On方法的jQuery文档中得到了很好的解释.