如何逃脱“:”? [英] How to escape ":"?
本文介绍了如何逃脱“:”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我的ID如
someform:somepanel:somebutton
someform:somepanel:somebutton
当我做jQuery(#someform:somepanel:somebutton)它返回某种形式,如何自动转义该id?
When I do jQuery("#someform:somepanel:somebutton") it returns someform, how to AUTOMATICALLY escape that id?
编辑:
我想做这样的事情
jQuery(somefunction("#someform:somepanel:somebutton"))
推荐答案
如果只是这个非常专门的版本,你可以只是 .replace()
b
$ b
If it's only this very specialized version, you can just .replace()
the character.
function somefunction(selector) {
return selector.replace(/:/, '\\\\:');
}
jQuery(somefunction("#someform:somepanel:somebutton"))
然后转换为
jQuery("#someform\\:somepanel\\:somebutton");
要拥有更通用的版本,可以使用正则表达式:
To have a more generic version, you can use a regexp:
function somefunction(selector) {
return selector.replace(/(!|"|#|\$|%|\'|\(|\)|\*|\+|\,|\.|\/|\:|\;|\?|@)/g, function($1, $2) {
return "\\\\" + $2;
});
}
这篇关于如何逃脱“:”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文