如何使用 $ 以及 jQuery
How to use $ as well as jQuery
我有一个 opencart 项目,我在其中使用了 $('#some-id')
和 jQuery('#some-id')
,它运行良好,但是在将项目从 1.5 升级到 2.1 之后,它只接受 jQuery('#some-id')
和$('#some-id')
作为
抛出错误
TypeError: $(...) is null
以下是我的一百个样本之一 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\']')
,它会移动到使用 $ 的其他行。例如见下图。
我尝试使用最新和旧版本的 jQuery,但我的问题仍然存在。
由于您没有包含有关代码的任何信息,这只是一般提示,但可能值得您深入研究:
在尝试防止不同 JS 框架和库(全部使用 $
快捷方式)之间发生冲突时,通常所做的是封装您自己的库或模块:
jQuery.noConflict();
(function($) {
// your library or modules here
})(jQuery);
这样标识符 jQuery
只需在您自己的代码之外指定一次,此时定义本身就是 "self calling" 本身。在该块内,jQuery 库现在再次在快捷方式 $
下已知,但它 不会 与该快捷方式的其他用法冲突 该代码块不再存在。
这是关于该主题的提示汇编:https://api.jquery.com/jquery.noconflict/
我有一个 opencart 项目,我在其中使用了 $('#some-id')
和 jQuery('#some-id')
,它运行良好,但是在将项目从 1.5 升级到 2.1 之后,它只接受 jQuery('#some-id')
和$('#some-id')
作为
TypeError: $(...) is null
以下是我的一百个样本之一 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\']')
,它会移动到使用 $ 的其他行。例如见下图。
我尝试使用最新和旧版本的 jQuery,但我的问题仍然存在。
由于您没有包含有关代码的任何信息,这只是一般提示,但可能值得您深入研究:
在尝试防止不同 JS 框架和库(全部使用 $
快捷方式)之间发生冲突时,通常所做的是封装您自己的库或模块:
jQuery.noConflict();
(function($) {
// your library or modules here
})(jQuery);
这样标识符 jQuery
只需在您自己的代码之外指定一次,此时定义本身就是 "self calling" 本身。在该块内,jQuery 库现在再次在快捷方式 $
下已知,但它 不会 与该快捷方式的其他用法冲突 该代码块不再存在。
这是关于该主题的提示汇编:https://api.jquery.com/jquery.noconflict/