在jQuery中将$与变量一起使用后,是否有任何特定的原因 [英] Is there any specific reason behind using $ with variable in jQuery
问题描述
我知道这是一个愚蠢的问题,但对此我有些困惑.例如,如果我有一个ID为rad1
的输入,下面的代码行之间有什么区别吗?
I know it's a silly question but I am a bit confused with this. For example, if I have an input with an ID: rad1
, is there any difference between below lines of code?
var $a = $('#rad1')
或
var a = $('#rad1')
推荐答案
没有实际区别.
这只是一个约定,可以帮助您记住a
不是DOM元素,而是jQuery对象.
It's just a convention that helps you remember that a
isn't the DOM element but it's a jQuery object.
var a = document.getElementById('a');
a.innerHTML //fine
var $a = $('#a');
$a.html() // fine
哦,顺便说一句,a
或$a
都不是好的变量名...您应该使用有意义的变量名,而不是abc字符.
Ohhh, and by the way, neither a
or $a
are good variable names ... you should use meaningful variable names not abc characters.
在同一网站上阅读 jQuery信息标签:
可变命名约定
jQuery包装的变量通常以'$'开头,以区别于标准JavaScript对象.
jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.
var $this = $(this);
这篇关于在jQuery中将$与变量一起使用后,是否有任何特定的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!