如何使用$以及jQuery

时间:2016-04-01 07:29:09

标签: jquery opencart

我有一个opencart项目,其中我使用$('#some-id')以及jQuery('#some-id')这是完美的工作,但在将项目从1.5升级到2.1后,它只接受jQuery('#some-id')并抛出错误$('#some-id')

  

TypeError:$(...)为空

以下是我的100个样本ajax函数之一。

    $('#button-coupon').on('click', function() {
        $.ajax({
            url: 'index.php?route=total/coupon/coupon',
            type: 'post',

/*Here $ is not working jQuery works. Not just here, but in each line where $ is used its throwing error, untill & unless I replace it with jQuery.*/
            data: 'coupon=' + encodeURIComponent($('input[name=\'coupon\']').val()),
            dataType: 'json',
            success: function(json) {
                $('.alert').remove();
            }
        });
    });

有没有办法让它们都起作用。我不想浪费时间用 jQuery 替换 $

以下是firebug控制台中的错误快照。

如果我将$('input[name=\'coupon\']')更改为jQuery('input[name=\'coupon\']'),则会将其移至使用 $ 的其他行。例如见下图。

next error after replacing $ with jQuery in above line.

我曾尝试使用最新版本和旧版本的jQuery,但我的问题仍然存在。

1 个答案:

答案 0 :(得分:2)

由于您没有包含有关您的代码的任何信息,这只是一般性提示,但可能值得为您深入探讨:

在尝试防止不同JS框架和库(所有使用$快捷方式)之间发生冲突时,通常会采用以下方式封装您自己的库或模块:

jQuery.noConflict();
(function($) {
    // your library or modules here
})(jQuery);

这样,当定义为“自我调用”时,标识符jQuery必须仅在您自己的代码之外指定一次。在该块中,jQuery库现在再次以快捷方式$知道,但它与该之外的 的其他用法冲突。

这是有关该主题的提示汇编:https://api.jquery.com/jquery.noconflict/

相关问题