首页 > 文章列表 > 为什么使用 $(...).on 时会报错“on is not a function”?

为什么使用 $(...).on 时会报错“on is not a function”?

480 2025-03-20

为什么使用 $(...).on 时会报错“on is not a function”?

使用 $(...).on 时报错“on is not a function”?

你在使用 $(...).on 时遇到了错误“on is not a function”。此错误表明你的 jQuery 版本可能太旧,不支持 on 方法。

要解决此问题,你可以尝试以下方法:

  • 检查 jQuery 版本:确保你使用的是最新版本的 jQuery。旧版本不支持 on 方法。
  • 使用 bind 或 live 方法:在旧版本的 jQuery 中,可以使用 bind 或 live 方法来实现类似于 on 的功能。

以下是如何使用 bind 方法替换 on 方法:

$("#btn").bind('click',function(){
    $('#dialog').show();
});

希望这些信息对你有帮助!

来源:1730172772