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

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

    I don't know much about jQuery, but try this:

    row_id = "#5";
    row = $("body").find(row_id);
    

    Edit: Of course, if the variable is a number, you have to add "#" to the front:

    row_id = 5
    row = $("body").find("#"+row_id);
    

提交回复
热议问题