What does the '#' sign mean in JavaScript?

后端 未结 8 1095
闹比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:14

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

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

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

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

    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, Mootools and 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.

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

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

    That's just a string. The # is just part of a string. I'm assuming the $ is 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.

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

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

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

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

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

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

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

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

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