在 jQuery 中使用带变量的 $ 背后是否有任何特定原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10204606/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:56:53  来源:igfitidea点击:

Is there any specific reason behind using $ with variable in jQuery

jquery

提问by Exception

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?

我知道这是一个愚蠢的问题,但我对此有点困惑。例如,如果我有一个带有 ID: 的输入,rad1下面的代码行之间有什么区别吗?

var $a = $('#rad1')

or

或者

var a = $('#rad1')

回答by gdoron is supporting Monica

No there is no real difference.

不,没有真正的区别。

It's just a convention that helps you remember that aisn't the DOM element but it's a jQuery object.

这只是一个约定,可以帮助您记住它a不是 DOM 元素,而是一个 jQuery 对象。

var a = document.getElementById('a');
a.innerHTML  //fine

var $a = $('#a');
$a.html()   // fine

Ohhh, and by the way, neither aor $aare good variable names ... you should use meaningful variable names not abc characters.

噢噢噢,顺便说一句,没有a$a有良好的变量名...你应该使用有意义的变量名不ABC字符。



Read the jQuery info tagon this very same site:

阅读同一个站点上的jQuery 信息标签

Variable Naming Conventions

变量命名约定

jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.

jQuery 包装的变量通常以“$”开头,以区别于标准 JavaScript 对象。

var $this = $(this);

回答by Chuck Norris

It's only for showing that it's a Jquery variable.

它只是为了表明它是一个 Jquery 变量。

Declaring $ayou're showing that your variable is for JQuery objects, it's just a notation. So the most readable thing will be to declare Jquery variable with $ notation

声明$a您正在展示您的变量用于 JQuery 对象,这只是一个符号。所以最易读的事情是用 $ 符号声明 Jquery 变量

var $obj=$("#obj");

And DOM element without $ notation

和没有 $ 符号的 DOM 元素

var obj = document.getElementById("obj");

回答by kinakuta

There's no difference. It's just a coding convention to help identify that the variable represents a jquery wrapped object.

没有区别。这只是一个编码约定,以帮助识别变量代表 jquery 包装的对象。

回答by Govind Malviya

No difference its just coding convention , check this

没有区别只是编码约定,检查这个

回答by ylev

A composition also works fine:

组合也可以正常工作:

You can also do something like this to show a <div>:

你也可以做这样的事情来显示<div>

function getCompTable(divId){
  var $d = $('#' + divId);
  $d.show();
}

USAGE

用法

getCompTable('compListDiv'); // compListDiv - is a div id=""

Yosi Lev

约西列夫

回答by Shiv

I think this scenario should illustrate the reason for remembering(off course, assigning '$' ) jquery and simple javascript variables:

我认为这种情况应该说明记住(当然,分配 '$' )jquery 和简单的 javascript 变量的原因:

<form id='myform' >
  <input id="name" type="text" value="Peter" />
</form>

 <script>

 $(document).ready(function(){
      var name = document.getElementById('name');
  var $name = $('#name');

  console.log(name.value);   // javascript's properties are available 
       console.log($name.value);  //it is undefined  (worth of notice)

  console.log(name.val());   // error!  name.val is not a function !  (jquery function will not be available)
  console.log($name.val());    // jquery functions are available

});

 </script>