我有这个代码:
var imgId = this.id; //file1.jpg
和这个:
$('.img\\' + imgId).remove();
所以问题是,我无法逃脱“。” 因为它在var中,而不是之前。我能做些什么?
我有这个代码:
var imgId = this.id; //file1.jpg
和这个:
$('.img\\' + imgId).remove();
所以问题是,我无法逃脱“。” 因为它在var中,而不是之前。我能做些什么?
不要将文件名作为类/id 的一部分存储在您的标记中。相反,使用 data- 属性:
<div class="img" data-filename="file1.jpg" />
然后在你的 jQuery 中:
$('.img[data-filename="'+imgId+'"]').remove();
我可能误解了这个问题 - 但是,这行得通吗?
$('.img\\' + replace(imgId,'.','\.')).remove();
你不能做类似的事情吗$("#"+imgId, ".img").remove()
用来.replace
代替它。
var imgId = this.id.replace('.', '/.'); //Or whatever other escaping is there.
虽然句点是 HTML Id 中的有效实体,但我强烈建议您遵循不同的命名约定,特别是在 Id 值中不使用句点的命名约定。