Home  >  Q&A  >  body text

求问jQuery中delegate 和on的区别?

// jQuery 1.4.3+
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7+
$( elements ).on( events, [selector], data, handler );

求问 delegate 和on的区别只是参数顺序吗?如果只是参数顺序的话,觉得没必要去更新方法哈


phpcn_u233phpcn_u2332808 days ago2009

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-10-01 00:40:58

    What is the difference between delegate and on in jQuery? -PHP Chinese website Q&A-What is the difference between delegate and on in jQuery? -PHP Chinese website Q&A

    Let’s take a look and learn.

    reply
    0
  • 阿神

    阿神2017-03-04 14:56:48

    官方文档已经不推荐使用.delegate()了

    .delegate() has been deprecated. It was superseded by the .on() method since jQuery 1.7, so its use was already discouraged.

    更新答案,题主举的例子中,结果是一样的:

    .on() 的语法有两种情况:

    1.此时效果跟.bind()、.click()是一样的。

    目标元素.on('click',handler);

    2.而下面这种情况,确实跟 .delegate()是一样的。          

    代理元素.on('目标元素选择器',handler);


    reply
    0
  • Cancelreply