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

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

    There are two problems with your code

    1. To find an element by ID you must prefix it with a "#"
    2. You are attempting to pass a Number to the find function when a String is required (passing "#" + 5 would fix this as it would convert the 5 to a "5" first)

提交回复
热议问题