I have this code:

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

and this:

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

So the problem is, i cant escape the '.' because its in the var, not before. What can i do?

有帮助吗?

解决方案

Don't store the filename as part of the class/id in your markup. Instead, use a data- attribute:

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

And then in your jQuery:

$('.img[data-filename="'+imgId+'"]').remove();

其他提示

I've probably misunderstood the question - but, would this work?

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

can't you do something like $("#"+imgId, ".img").remove()

Use .replace to replace it.

var imgId = this.id.replace('.', '/.'); //Or whatever other escaping is there.

While the period is a valid entity in an HTML Id I strongly sugest you follow a different naming convention, specifically one where periods are not used in Id values.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top