简体   繁体   English

什么时候应该在$上使用jQuery?

[英]When, if ever, should you use `jQuery` over `$`?

I was looking through some code and noticed: 我正在查看一些代码,发现:

button: function(e) {
  e.preventDefault();
  var $target = jQuery(e.target);
  var link = $target.attr('href');

I was just a little unclear about the line var $target = jQuery(e.target); 我只是不清楚var $target = jQuery(e.target); .

Why use jQuery here? 为什么在这里使用jQuery

Usually you would like to use jQuery instead of $ , when the latter conflicts with a globar variable from another library. 通常,当$与另一个库中的globar变量冲突时,通常要使用jQuery而不是$

For instance, see this list: What JavaScript libraries are known to use the global dollar sign: window.$? 例如,请参见以下列表: 已知哪些JavaScript库使用全局美元符号:window。$?

In that case, jQuery provides the noConflict() method, which : 在这种情况下,jQuery提供了noConflict()方法, 方法:

Relinquish jQuery's control of the $ variable. 放弃jQuery对$变量的控制。

The documentation also states: 该文档还指出:

In jQuery's case, $ is just an alias for jQuery , so all functionality is available without using $ . 在jQuery的情况下, $只是jQuery的别名,因此无需使用$即可使用所有功能。 If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict() . 如果您需要使用其他JavaScript库jQuery的旁边,返回控制$有一个回调到其他图书馆$.noConflict() Old references of $ are saved during jQuery initialization; $旧引用在jQuery初始化期间保存; noConflict() simply restores them. noConflict()只是还原它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM