简体   繁体   English

javascript字符串串联到jquery选择器

[英]javascript string concatenation to jquery selector

I know its a silly question to ask, but may be I am missing something. 我知道这是一个愚蠢的问题,但是可能我错过了一些东西。 So, I have 所以我有

data = "ajey"

and then, 接着,

I want to select an li item from ul whose id is "ajey" 我想从id为"ajey" ul选择一个li项目。

I am trying with 我正在尝试

$('#users li#data').css('background-color', 'red');

but this returns an empty array. 但这将返回一个空数组。 I know the mistake is in li#data 我知道错误在于li#data

#users is the id of the ul element #users是ul元素的ID

Any help is greatly appreciated. 任何帮助是极大的赞赏。

由于ID是唯一的,因此您可以:

$('#' + data).css('background-color', 'red');

You can just concatenate the strings with + as you would normally do: 您可以像通常那样用+连接字符串:

$('#users li#' + data).css('background-color', 'red');

If you are doing this, make sure that the value of data is trusted. 如果这样做,请确保data值是可信的。

$('#users li#'+data).css('background-color', 'red');

采用

$('#users li#' + data).css('background-color', 'red');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM