Is there any specific reason behind using $ with variable in jQuery

后端 未结 6 572
天涯浪人
天涯浪人 2020-11-27 16:10

I know it\'s a silly question but I am a bit confused with this. For example, if I have an input with an ID: rad1, is there any difference between below lines o

6条回答
  •  醉话见心
    2020-11-27 16:35

    No there is no real difference.

    It's just a convention that helps you remember that a isn't the DOM element but it's a jQuery object.

    var a = document.getElementById('a');
    a.innerHTML  //fine
    
    var $a = $('#a');
    $a.html()   // fine
    

    Ohhh, and by the way, neither a or $a are good variable names ... you should use meaningful variable names not abc characters.


    Read the jQuery info tag on this very same site:

    Variable Naming Conventions

    jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.

    var $this = $(this);
    

提交回复
热议问题