What does the '#' sign mean in JavaScript?

后端 未结 8 1097
闹比i
闹比i 2021-02-07 20:48

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.

$(function ()         


        
相关标签:
8条回答
  • 2021-02-07 21:36

    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".

    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.

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

    0 讨论(0)
  • 2021-02-07 21:37

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

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

    0 讨论(0)
提交回复
热议问题