如何在jQuery中传递事件和$作为参数(在wordpress中启用了noconflict模式)

How to pass event and $ as parameter in jQuery (noconflict mode enabled in wordpress)

本文关键字:wordpress 参数 启用 noconflict 模式 jQuery 事件      更新时间:2023-09-26

有问题的代码在这里:

https://github.com/louisremi/WordPress-Sortable-Posts/blob/master/script.js

这在最新的wordpress中不起作用(没有定义$),所以我在每个函数中都传递$。这个功能让人头疼:

jQuery(document).on('mousemove', function(e) {

我也想使用$作为参数:

jQuery(document).on('mousemove', function(e , $ ) {

但其抛出错误:e/$未定义。有什么办法克服这一点吗?

您可以将jQuery作为参数封装在IIFE中的代码

(function($){
    $(document).on('mousemove', function(e) {
    ....
    });
}(jQuery));

您可以使用包装器:

(function($) {
    $(document).on('mousemove', function(e , $ ) {
    });
})(jQuery);

法典中简要讨论了"包装方法"。