search

Home  >  Q&A  >  body text

javascript - Simple little question about selectors

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

三叔三叔2717 days ago901

reply all(6)I'll reply

  • 怪我咯

    怪我咯2017-06-21 10:14:21

    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.

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-21 10:14:21

    add_button 这样就不报错了
    
    变量命名规则,字母或者_打头,后面放什么数字字母下划线,命名规则里面没有`-`连字符

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-21 10:14:21

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

    reply
    0
  • 为情所困

    为情所困2017-06-21 10:14:21

    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.

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-21 10:14:21

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

    in the middle

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-21 10:14:21

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

    reply
    0
  • Cancelreply