开发者

Passing an escaped String to JQuery($)-Function

currently I am trying to highlight elements on a page. Therefore I pass a comma seperate String to a Javascript-Funktion called highlight.

highlight("main:box1,main:box2");

This was working fine till I found ids with : on the page. So I tried to escape them with a little regex. Here things started to get a little funny.

If I escape the string by replacing : with \: the jQuery-Function does not work anymore.

var string = value.replace(/:/g, "\\\\:开发者_如何学运维");
jQuery("#" + string).css("color", "red");

If I replace main: with "" and write main\: in the jQuery-Function everything works fine.

var string = value.replace(/main:/g, "");
jQuery("#main\\:" + string).css("color", "red");

What am I doing wrong? Why does the jQuery-Function not except my escaped string?

Help needed :-(

Example-Code attached: http://db.tt/0FLRlM

Thanks Jan


You're double escaping the \ in your first attempt at substitution. What you've done is replace : with \\:, even though you're probably seeing \: when you output it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜