jQuery的两个定义准备好了

Two definitions of jQuery ready

我正在尝试调试 Wordpress 站点上的一个奇怪行为:ready 第一次出现时的代码似乎没有发生。由于安装了几个插件,我在代码中看到了这个:

<script type="text/javascript">
jQuery(document).ready(function(){   (function() {   ... })();});
</script>

后来,几乎在页面末尾我看到:

<script type="text/javascript">
jQuery(document).on('ready post-load', easy_fancybox_handler );
</script>

一个定义或 "ready" 会覆盖另一个定义还是两者都会发生?

documented,

When multiple functions are added via successive calls to this method, they run when the DOM is ready in the order in which they are added.

所以设置一个 ready 处理程序两次就可以了。

但是:

  • 第一个缺少尾随 }),应该是语法错误(这可能是它不执行的原因)。

  • 第二个使用 jQuery(document).on('ready') 重载,已弃用:

    There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.