JavaScript 中的“#”符号是什么意思?

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

What does the '#' sign mean in JavaScript?

javascript

提问by Bastien Vandamme

I'm new to Javascript and I'm try to understand some code. I don't understand and I can't find any documentation about the #sign.

我是 Javascript 的新手,我试图理解一些代码。我不明白,也找不到有关该#标志的任何文件。

$(function () {
      $("#searchTerm").autocomplete({

What does $("#searchTerm") mean?

$("#searchTerm") 是什么意思?

回答by Quentin

In JavaScript? Nothing special. It is just part of a string.

在 JavaScript 中?没什么特别的。它只是字符串的一部分。

The $function might do something with it, but it is hard to tell what the $function is.

$功能可能会用它做什么,但它是很难说的什么$功能

There are a lot of libraries which provide a $function that acts as a kitchen sink for that library. They include Prototype, Mootoolsand jQuery. This one looks most like jQuery, in which case the argument is a string containing a CSS selector, so the #indicates the start of an id selector.

有很多库提供了$作为该库的厨房水槽的功能。它们包括PrototypeMootoolsjQuery。这个看起来最像 jQuery,在这种情况下,参数是一个包含CSS 选择器的字符串,因此#指示id 选择器的开始。

This "Selects a single element with the given id attribute".

这“选择具有给定 id 属性的单个元素”。

回答by j08691

That's jQuery and the pound sign (#) refers to an element's ID. It's one way jQuery can select an element. In your example, it would select the element with the ID of "searchTerm".

那是 jQuery,井号 (#) 指的是元素的 ID。这是 jQuery 选择元素的一种方式。在您的示例中,它将选择 ID 为“searchTerm”的元素。

For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.

As always, remember that as a developer, your time is typically the most valuable resource. Do not focus on optimization of selector speed unless it is clear that performance needs to be improved.

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

If the id contains characters like periods or colons you have to escape those characters with backslashes.

对于 id 选择器,jQuery 使用 JavaScript 函数 document.getElementById(),这是非常有效的。当另一个选择器附加到 id 选择器时,例如 h2#pageTitle,jQuery 在将元素识别为匹配之前执行额外的检查。

一如既往,请记住,作为开发人员,您的时间通常是最宝贵的资源。不要专注于优化选择器速度,除非很明显性能需要改进。

每个 id 值在一个文档中只能使用一次。如果多个元素被分配了相同的 ID,则使用该 ID 的查询将只选择 DOM 中第一个匹配的元素。但是,不应依赖这种行为;具有多个元素使用相同 ID 的文档是无效的。

如果 id 包含句点或冒号等字符,则必须使用反斜杠转义这些字符。

See: http://api.jquery.com/id-selector/

请参阅:http: //api.jquery.com/id-selector/

回答by Heitor Chang

With the given information, it is most likely the jQuery ID selector

根据给定的信息,很可能是 jQuery ID 选择器

http://api.jquery.com/id-selector/

http://api.jquery.com/id-selector/

回答by celsowm

Now # would/could mean private instance fields: https://tc39.github.io/proposal-class-fields/

现在 # 将/可能意味着私有实例字段:https: //tc39.github.io/proposal-class-fields/

回答by Rocket Hazmat

That's just a string. The #is just part of a string. I'm assuming the $is jQuery.

那只是一个字符串。该#是一个字符串的一部分正好。我假设$是 jQuery。

That means, that the string is a jQuery selector (or rather a CSS selector). The #means "ID". It's searching the DOM for the element with the ID `searchTerm.

这意味着,该字符串是一个 jQuery 选择器(或者更确切地说是一个 CSS 选择器)。的#手段“ID”。它正在 DOM 中搜索 ID 为“searchTerm”的元素。

回答by Florian Salihovic

That's the id selector for elements in HTML (in the DOM to be specific).

那是 HTML 中元素的 id 选择器(在 DOM 中是特定的)。

回答by Florian Salihovic

That isn't vanilla Javascript! That's jQuery!

那不是普通的 Javascript!那是jQuery!

In jQuery you can select elements via CSS style selectors. In this case, #xis a CSS selector to select all elements with the id x.

在 jQuery 中,您可以通过 CSS 样式选择器选择元素。在这种情况下,#x是一个 CSS 选择器,用于选择所有具有 id 的元素x

回答by user3313344

It's an element ID e.g: `...

这是一个元素 ID,例如:`...

When u need to access this div with JS or jQuery just call it $("#xyz").do something

当您需要使用 JS 或 jQuery 访问此 div 时,只需调用它。$("#xyz")做点什么

for class <div class="abc">....</div>>> $(".abc")

班级<div class="abc">....</div>>>$(".abc")