Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

Jquery delegate()

delegate()方法用于为指定元素(它们是选定元素的子元素)附加一个或多个事件处理程序。该方法执行事件发生时要运行的功能。
附带有delegate()方法的事件处理程序适用于当前和将来的元素。
语法:
$(selector).delegate(childSelector,event,data,function)

jQuery Representative()事件的参数

参数 说明
ChildSelector 这是一个强制性参数,用于指定一个或多个子元素来附加事件处理程序。
event 它也是必填参数。它指定一个或多个事件附加到元素。如果您使用多个事件,那么它们必须用空格隔开。
data 它是可选的,并指定要传递给该函数的其他数据。
function 事件发生时执行。
我们来看一个jQuerydelegate()示例。
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>delegate demo</title>
  <style>
  p {
    background: yellow;
    font-weight: bold;
    cursor: pointer;
    padding: 5px;
  }
  p.over {
    background: #ccc;
  }
  span {
    color: red;
  }
  </style>
  <script src="/jquery-1.10.2.js"></script>
</head>
<body>
<p>This is a single paragraph. Click me for next.</p>
<script>
$("body").delegate("p", "click", function() {
  $(this ).after("<p>Click me for another paragraph.</p>");
});
</script>
</body>
</html>
输出:

This is a single paragraph. Click me for next.

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4