如何通过使用ID变量来选择jQuery中的元素? [英] How do I select an element in jQuery by using a variable for the ID?

查看:72
本文介绍了如何通过使用ID变量来选择jQuery中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,以下代码选择id ="2"的部门:

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

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

我该怎么做:

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

以上语法产生错误.我检查了jQuery文档并在这里回答不成功.

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

推荐答案

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

更重要的是,执行额外的body.find对性能没有影响.正确的方法很简单:

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

row = $('#' + row_id);

这篇关于如何通过使用ID变量来选择jQuery中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆