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

前端 未结 6 870
野的像风
野的像风 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:15

    The shortest way would be:

    $("#" + row_id)
    

    Limiting the search to the body doesn't have any benefit.

    Also, you should consider renaming your ids to something more meaningful (and HTML compliant as per Paolo's answer), especially if you have another set of data that needs to be named as well.

提交回复
热议问题