如何使用ID的变量来选择jQuery中的元素?

时间:2022-04-20 20:20:05

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

例如,下面选择id="2"的除法:

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

How do I do something like this:

我该怎么做:

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

The above syntax produces an error. I checked the jQuery documentation and answers here without success.

上面的语法会产生错误。我在这里检查了jQuery文档和答案,但没有成功。

6 个解决方案

#1


156  

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

More importantly doing the additional body.find has no impact on performance. The proper way to do this is simply:

更重要的是做额外的身体。发现对性能没有影响。正确的做法很简单:

row = $('#' + row_id);

#2


39  

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.

另外,您应该考虑将您的id重新命名为更有意义的东西(和按照Paolo的答案兼容HTML),特别是如果您有另一组需要命名的数据。

#3


26  

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

做$(“身体”);();用ID查找时不需要;没有性能增益。

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

请注意,以数字开头的ID不是有效的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 (".").

ID和NAME标记必须以字母开头([a- za -z]),后面可能会有任意数量的字母、数字([0-9])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#4


5  

You can do it like this:

你可以这样做:

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

#5


1  

There are two problems with your code

代码有两个问题

  1. To find an element by ID you must prefix it with a "#"
  2. 要通过ID查找元素,必须在其前面加上“#”
  3. 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)
  4. 当需要字符串时,您正在尝试将数字传递给find函数(传递“#”+ 5将修复这个问题,因为它将首先将5转换为“5”)

#6


0  

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

我不太了解jQuery,但是试试这个:

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);

#1


156  

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

More importantly doing the additional body.find has no impact on performance. The proper way to do this is simply:

更重要的是做额外的身体。发现对性能没有影响。正确的做法很简单:

row = $('#' + row_id);

#2


39  

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.

另外,您应该考虑将您的id重新命名为更有意义的东西(和按照Paolo的答案兼容HTML),特别是如果您有另一组需要命名的数据。

#3


26  

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

做$(“身体”);();用ID查找时不需要;没有性能增益。

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

请注意,以数字开头的ID不是有效的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 (".").

ID和NAME标记必须以字母开头([a- za -z]),后面可能会有任意数量的字母、数字([0-9])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#4


5  

You can do it like this:

你可以这样做:

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

#5


1  

There are two problems with your code

代码有两个问题

  1. To find an element by ID you must prefix it with a "#"
  2. 要通过ID查找元素,必须在其前面加上“#”
  3. 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)
  4. 当需要字符串时,您正在尝试将数字传递给find函数(传递“#”+ 5将修复这个问题,因为它将首先将5转换为“5”)

#6


0  

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

我不太了解jQuery,但是试试这个:

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);