javascript - Simple little question about selectors
三叔
三叔 2017-06-21 10:12:21
0
6
844

Why does the following selector always report an error

var add-button=$('button[name=add]')

The error will not be reported if the variable name is changed

var add=$('button[name=add]')

Beginners encounter small problems when writing js, I hope someone can help me solve them

三叔
三叔

reply all (6)
刘奇

Variable names are case-sensitive and are allowed to contain letters, numbers, dollar signs ($), and underscores, but the first character is not allowed to be a number. Spaces and other punctuation marks are not allowed. JavaScript keywords and reserved words are not allowed in variable names. full name. add(-)button belongs to other symbols.

    扔个三星炸死你
    add_button 这样就不报错了 变量命名规则,字母或者_打头,后面放什么数字字母下划线,命名规则里面没有`-`连字符
      漂亮男人

      The two people above are right. It is best for beginners to understand the naming rules and habits

        为情所困

        Identifier is a name used to identify a specific object. The most common identifiers are variable names, and function names to be mentioned later. JavaScript language identifiers are case-sensitive, so a and A are two different identifiers.

        Identifiers have a set of naming rules. Those that do not comply with the rules are illegal identifiers. The JavaScript engine will report an error when it encounters an illegal identifier.

        Simply put, the identifier naming rules are as follows:

        The first character can be any Unicode letter (including English letters and letters from other languages), as well as the dollar sign ($) and the underscore (_).
        The second character and subsequent characters, in addition to Unicode letters, dollar signs and underscores, can also use numbers 0-9.
        The following are legal identifiers.

        arg0 _tmp $elem π

        The following are illegal identifiers.

        1a // 第一个字符不能是数字 23 // 同上 *** // 标识符不能包含星号 a+b // 标识符不能包含加号 -d // 标识符不能包含减号或连词线

        Chinese is a legal identifier and can be used as a variable name.

        var 临时变量 = 1;

        JavaScript has some reserved words that cannot be used as identifiers: arguments, break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, eval, export, extends, false, finally, for ,function,if,implements,import,in,instanceof,interface,let,new,null,package,private,protected,public,return,static,super,switch,this,throw,true,try,typeof,var,void , while, with, yield.

        In addition, although there are three words that are not reserved words, they should not be used as identifiers because they have special meanings:Infinity, NaN, and undefined.

          世界只因有你

          The identifier does not conform to the specification
          add-buttonThe-illegal characters

          in the middle
            習慣沉默

            It’s best to use _, don’t use -

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!