The $ dollar sign

后端 未结 9 1777
执笔经年
执笔经年 2020-11-29 04:16

I have something simple like this:

$(selector).append("somestuff");

But since I\'m going to reuse the selector I cache it with:

相关标签:
9条回答
  • 2020-11-29 04:21

    I don't believe that you need to add the jQuery selector to jQuery objects (since it's already a part of the object). We don't add the selector and haven't run into any problems.

    0 讨论(0)
  • 2020-11-29 04:25

    $ is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.

    Whether you use a dollar sign in your name isn't relevant to jQuery - there's nothing special about the dollar sign, except that jQuery defines a function called $.

    0 讨论(0)
  • 2020-11-29 04:25

    I like to prefix all my jQuery object names with $ so that I know it's actually a jQuery object, not a DOM reference.

    It's a good naming convention.

    0 讨论(0)
  • 2020-11-29 04:38

    "$" is a function in jQuery. So when you call $(selector), you're actually calling the function $ with selector as the argument.

    Generally, don't use the "$" as part of a variable name for javascript. You will only confuse yourself.

    0 讨论(0)
  • 2020-11-29 04:40

    I've seen it done both ways. All you are doing is creating a variable with the name '$selector', so they are functionally equivalent. The nice thing about it is that it does make them easy to pick out as jQuery objects.

    0 讨论(0)
  • 2020-11-29 04:40

    RichieHindle is correct. To expand:

    Javascript variables allow the '$' character. So for example, you could have the following:

    var $i = 1;
    var i = 1;
    

    Both i and $i have the same value, and both are perfectly legitimate variable names.

    jQuery assigns itself (the jQuery object) to '$' because traditionally, that's what Javascript frameworks have done for selectors. There's no inherent meaning to '$' beyond what jQuery gives it.

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