如何将自定义属性值设置为等于变量?
|
这是我的jQuery:
if ($(\'div[data-sort= 23 ]\').length > 0) {
$(\'div[data-sort= 23 ]\').after(newdiv);
}
如果我要将数字23设置为等于n,这就是我所做的:
n = 23;
if ($(\'div[data-sort= n ]\').length > 0) {
$(\'div[data-sort= n ]\').after(newdiv);
}
但这是行不通的。谁能告诉我为什么?
没有找到相关结果
已邀请:
1 个回复
森含械
您需要使变量成为字符串的一部分;在此新代码中,将数字
连接到字符串中,以便jQuery的选择器起作用。要查看jQuery作为选择器收到的内容,可以运行以下命令:
您应该看到\“ div [data-sort = 23] \”。