Jquery 变量前面的$是什么意思

Jquery 变量前面的$是什么意思,jquery,Jquery,它前面的$dollar符号用于选择当前元素。我对$front-of选项和thisSelect有点困惑。它们有特殊的意义吗 var $options = $(this).children('option'); var $thisSelect = $(this); 感谢您的帮助,美元符号可以帮助指示变量何时包含jQuery对象,而不是任何其他类型的对象。如果编码人员希望在var前面包含$sign或不包含$sign,这完全取决于编码人员,它只是作为一个提醒。没有任何意义。这些只是普通的字符,就像u或

它前面的$dollar符号用于选择当前元素。我对$front-of选项和thisSelect有点困惑。它们有特殊的意义吗

var $options = $(this).children('option');
var $thisSelect = $(this);

感谢您的帮助,

美元符号可以帮助指示变量何时包含jQuery对象,而不是任何其他类型的对象。如果编码人员希望在var前面包含$sign或不包含$sign,这完全取决于编码人员,它只是作为一个提醒。

没有任何意义。这些只是普通的字符,就像u或π这个字符,如果你不掌握你的工具链,你就不安全,你可以把它放在你的变量名中

看。尤其是:

本标准规定了特定的字符添加:美元符号 $和下划线uu在 我的名字

你也可能对你的工作感兴趣


通常使用$variables作为包含jQuery集的前缀。

这是对jQuery包装对象的常见引用。它使阅读代码更容易知道哪些变量是jQuery包装的

//Item has been "cached" for later use in the script as a jQuery object.
var $item = $(this);
其他常见做法:

如果变量是私有的,则使用如下下划线:

(function(){
     var _foo = "bar";
})()
如果是公共的,我们不使用下划线:

var foo = "bar"
如果是jQuery选择器,则使用$:


这只是一种编码约定,它允许您快速引用后面代码中变量的类型。

$sign在这些变量名称之前与变量名称中的其他字符一样。这没有任何意义。您可以使用此约定来标识此变量中是否有jQuery对象。

有些人习惯于添加约定,在变量名称前添加$,以知道其值是jQuery对象

这样我就知道以下变量有不同的结果

var $this = $(this);
var self = this;

正如大家所说,这只是一个惯例。 我使用变量前面的$符号来标识这个变量包含一个对象

var thisIsANumber = 1024; // No $.. Its a normal variable
var $divElement = $('div#idOfDiv'); // Yes! Its a jQuery Object
var $this = $(this); // Commonly used to reduce the work javascript has to do!

//Now I can use something like this.. (Notice how easy it is to read!)
$divElement.slideUp();

// Or a more `real world` example!
$('#element').click(function(){
    // Hold $(this) inside a variable
    // So we don't have to traverse the dom unnecessarily
    var $this = $(this); // Save it (its a object.. so prepend a `$` )
    $this.hide(); // Use it again
    $this.fadeIn(); // and again
//  ^ Has a dollar sign, because it is a jQuery Object.
});
你会看到很多插件都很好地使用了这个约定。。至少是那些写得好的。 通过将对象存储在变量中,Javascript不必每次都在代码中爬行来获取元素。相反,我们在变量中已经有了元素,所以我们使用它来引用它


如果在同一回调函数中多次使用$this,则应将其存储在变量中。。var$this=$this;。否则,每次使用它时,javascript都必须从源代码中获取元素,这会大大降低性能!特别是对于在慢速/旧电脑上浏览的用户

前面的$美元符号用于选择当前元素。。。错。$是一个普通的函数调用。我相信我已经看到并回答了这个问题。。。但我不擅长搜索…为什么要否决这个问题?
var thisIsANumber = 1024; // No $.. Its a normal variable
var $divElement = $('div#idOfDiv'); // Yes! Its a jQuery Object
var $this = $(this); // Commonly used to reduce the work javascript has to do!

//Now I can use something like this.. (Notice how easy it is to read!)
$divElement.slideUp();

// Or a more `real world` example!
$('#element').click(function(){
    // Hold $(this) inside a variable
    // So we don't have to traverse the dom unnecessarily
    var $this = $(this); // Save it (its a object.. so prepend a `$` )
    $this.hide(); // Use it again
    $this.fadeIn(); // and again
//  ^ Has a dollar sign, because it is a jQuery Object.
});