为什么这个带有变量的 JQUERY 选择器和 space 不起作用?

Why is this JQUERY selector with a varibale and space not working?

 $(".lookup "+id).css("background-color","green");

我想在找到匹配项时将 "lookup 123456" 的背景更改为绿色。我将其写入控制台:console.log(".lookup"+id) 它工作正常。但是它没有被选择器选中。

有什么帮助吗?

使用以下代码:

$("[class='lookup "+id+"']").css();

选择器不能以数字开头。

http://www.w3.org/TR/CSS21/syndata.html#characters

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B WF".

Which characters are valid in CSS class names/selectors?

我正在考虑变量 id 保存元素的 id。

假设 id="something"

那么如果你使用你的代码

 $(".lookup "+id).css("background-color","green");

等于

 $(".lookup something").css("background-color","green");

某物不是元素 id 应该有 # 到 select。 所以试试这个代码

 $(".lookup #"+id).css("background-color","green");

这将是

 $(".lookup #something").css("background-color","green");

我不太清楚你在找什么。

如果您想要 select 个带有 class="lookup 123456" 的元素,那么您应该使用 . 而不是 space。示例:

$('.lookup.' + id);

如果您想要 select 带有 class="lookup" id="123456" 的元素,那么您应该使用 # 而不是 space。示例:

$('.lookup#' + id);

您当前的 selector 意思是 select 类似这样。

<div class="lookup">
  <123456></123456>
</div>