ID中的空格和ID jquery中的变量 [英] spaces in ID and putting variable in ID jquery
问题描述
我想在id中放置空格,并且我想使用变量选择一个id.
I want to put spaces in id and also i want to select an id using variable.
我想要获得的ID是这样的
<span id="printroofG.I. Sheet" class="underline"></span>
The id i want to achieve is something like this
<span id="printroofG.I. Sheet" class="underline"></span>
让我说我有这个变量.
var id = G.I. Sheet
;
这是我的选择器
$('span[id=printroof'+value.material+']').text(value.material);
我得到了无法识别的表达式:span [id = printroofG.I.工作表].
I get unrecognized expression: span[id=printroofG.I. Sheet].
在ID
中包含空格并将变量用作选择器的正确方法是什么.
What is the proper way to have spaces in ID
and use variable as selector.
推荐答案
正如我之前所说,请勿为此使用ID
.最好使用如下所示的data-*
.
As I said earlier, do not use an ID
for this purpose. You'd be better of using data-*
as below.
var id = "G.I. Sheet";
$('span[data-id="printroof' + id + '"]').text("Some text");
//OR
$('span[data-id="printroof' + value.material + '"]').text(value.material);
HTML:
<span data-id="printroofG.I. Sheet" class="underline"></span>
这篇关于ID中的空格和ID jquery中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!