将“$”(美元符号)替换为“JQuery”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6746352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace "$"(dollar Sign) with "JQuery"
提问by Acubi
The 1st snippet wasn't working. However, it start working when replacing all $(dollar sign) with jQuery(See 2nd snippet). But i don't really understant why? Can anyone explain this to me? Many thanks!
第一个片段不起作用。但是,它在用jQuery替换所有$(美元符号)时开始工作(参见第二个片段)。但我真的不明白为什么?任何人都可以向我解释这一点吗?非常感谢!
1st Snippet
第一个片段
jQuery.noConflict();
$(document).ready(function(){
$("#insideTable > tbody > tr:odd").addClass("odd");
$("#insideTable > tbody > tr:not(.odd)").hide();
$("#insideTable > tbody > tr:odd").show();
$("#insideTable > tbody > tr.odd").click(function(){
$(this).next().toggle();
$(this).find(".arrow").toggleClass("up");
});
});
2nd Snippet
第二个片段
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery("#insideTable > tbody > tr:odd").addClass("odd");
jQuery("#insideTable > tbody > tr:not(.odd)").hide();
jQuery("#insideTable > tbody > tr:odd").show();
jQuery("#insideTable > tbody > tr.odd").click(function(){
jQuery(this).next().toggle();
jQuery(this).find(".arrow").toggleClass("up");
});
});
回答by Nicola Peluchetti
This is because jQuery.noConflict()
"frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict()
you can't do that anymore and so you have to replace each "$" with "jQuery"; .
这是因为jQuery.noConflict()
“释放”了与 jQuery 关联的“$”。通常在您的代码中,您可以使用 $ 作为“jQuery”的替代品。如果你使用noConflict()
你就不能再这样做了,所以你必须用“jQuery”替换每个“$”;.
Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():
许多 JavaScript 库使用 $ 作为函数或变量名,就像 jQuery 一样。在 jQuery 的情况下,$ 只是 jQuery 的别名,因此所有功能都可用而无需使用 $。如果我们需要在 jQuery 旁边使用另一个 JavaScript 库,我们可以通过调用 $.noConflict() 将 $ 的控制权返回给另一个库:
you can also create a totally new alias to use
您还可以创建一个全新的别名来使用
var myJqueryAlias = jQuery.noConflict();
myJqueryAlias(document).ready(function(){
myJqueryAlias("#insideTable > tbody > tr:odd").addClass("odd");
myJqueryAlias("#insideTable > tbody > tr:not(.odd)").hide();
myJqueryAlias("#insideTable > tbody > tr:odd").show();
myJqueryAlias("#insideTable > tbody > tr.odd").click(function(){
myJqueryAlias(this).next().toggle();
myJqueryAlias(this).find(".arrow").toggleClass("up");
});
});
回答by shanethehat
Calling noConflict() removes the association between the $ and the jQuery function. This is so that you can use another JavaScript library that also shortens to $ without conflicts.
调用 noConflict() 会删除 $ 和 jQuery 函数之间的关联。这样您就可以使用另一个同样缩短为 $ 的 JavaScript 库而不会发生冲突。