简体   繁体   English

此运算符(= $)在Javascript中是什么意思?

[英]What does this operator (=$) mean in Javascript?

I have seen code online using this operator and I don't know what it's supposed to mean: 我已经使用此运算符在线查看了代码,但我不知道这意味着什么:

var div1  =$  ('#div1');
var div2  =$  ('#div2');
var div3  =$  ('#div3');

It's returning me the element with the corresponding IDs, but why? 它向我返回具有相应ID的元素,但是为什么呢? How does it work and is it native? 它是如何工作的?

It means: 它的意思是:

var div1 = $('#div1');

Just poor formatting by whoever wrote that. 无论谁写的格式都不好。

Looks like the spacing is just not good. 看起来间距不是很好。 It is actually 实际上是

var div = $('#div1') 

a jQuery selector for element with id div1 一个ID为div1元素的jQuery选择器

It is not correct formatting. 格式不正确。 Мore correctly write like this: 正确地这样写:

var div1 = $('#div1')

The jQuery library exposes its methods and properties via two properties of the window object called jQuery and $ . jQuery库通过窗口对象的两个属性jQuery和$公开其方法和属性。 $ is simply an alias for jQuery and it's often employed because it's shorter and faster to write. $只是jQuery的别名,它经常被使用,因为它编写起来更短,更快。

In plain JS you can write: 在普通的JS中,您可以编写:

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

Its basically selecting the divs like $('#div1'), $('#div1') so on, just formatted wrongly. 它基本上选择了像$('#div1'),$('#div1')之类的div,只是格式错误。 That is why you are receiving element with the corresponding IDs 这就是为什么您要接收具有相应ID的元素

This is id of one of the div element in HTML code, Which can be used to define actions on that div element like: 这是HTML代码中div元素之一的ID,可用于在该div元素上定义操作,例如:

 var div1 = $('#div1'); div1.show(); div1.empty(); 

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

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