How do I select an element in jQuery by using a variable for the ID?

前端 未结 6 1081
野的像风
野的像风 2021-01-30 03:59

For example, the following selects a division with id=\"2\":

row = $(\"body\").find(\"#2\");

How do I do something like this:

r         


        
6条回答
  •  梦如初夏
    2021-01-30 04:09

    Doing $('body').find(); is not necessary when looking up by ID; there is no performance gain.

    Please also note that having an ID that starts with a number is not valid HTML:

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

提交回复
热议问题