0

我有这个代码:

var imgId = this.id; //file1.jpg

和这个:

$('.img\\' + imgId).remove();

所以问题是,我无法逃脱“。” 因为它在var中,而不是之前。我能做些什么?

4

5 回答 5

4

不要将文件名作为类/id 的一部分存储在您的标记中。相反,使用 data- 属性:

<div class="img" data-filename="file1.jpg" />

然后在你的 jQuery 中:

$('.img[data-filename="'+imgId+'"]').remove();
于 2011-09-12T16:09:22.720 回答
0

我可能误解了这个问题 - 但是,这行得通吗?

$('.img\\' + replace(imgId,'.','\.')).remove();
于 2011-09-12T16:07:51.650 回答
0

你不能做类似的事情吗$("#"+imgId, ".img").remove()

于 2011-09-12T16:07:57.093 回答
0

用来.replace代替它。

var imgId = this.id.replace('.', '/.'); //Or whatever other escaping is there.
于 2011-09-12T16:08:30.723 回答
0

虽然句点是 HTML Id 中的有效实体,但我强烈建议您遵循不同的命名约定,特别是在 Id 值中不使用句点的命名约定。

于 2011-09-12T16:09:28.960 回答