开发者

Is there a plugin that makes the jQuery id selector dot-safe?

I already know how to select DOM elements with IDs which contain dots, and I've read all the related answers on stackoverflow (like this: How to s开发者_如何学编程elect html nodes by ID with jquery when the id contains a dot?), but the problem is there's lots of plugins out there that are not aware of this issue, and short of going through the code of all the plug-ins we're using and replace the selectors appropriately, I was thinking that maybe there's another plug-in/extension for jQuery which replaces the main selector mechanism to make it dot-safe. I am using plug-ins like jqGrid and I've seen many places in the code where something like this is used:

$('#' + id).somefunction()

Any ideas?

Thanks in advance


You can alter the $.fn.init function to change the behavior of jQuery, but there isn't a quick and easy way to make sure IDs with dots in them are safe throughout your application. A quick and dirty solution would replace all .s with \\. by altering the init function, but then all your class selectors break.

The only solid way to do it is passing in the IDs with escaped dots to all your plugins.

EDIT

If you do want a quick and dirty solution, here's one that will at least work as long as you don't have something like #id.class. It will work for #id .class (the space in between).

(function($){
 $.fn._init = $.fn.init;
 $.fn.init = function ( selector, context, rootjQuery ) {
  if (typeof selector == 'string' && selector.match('#')) {
   selector = selector.replace(/\#([^\s\.]*)\./g, '#$1\\.')
  }
  return new $.fn._init(selector,context,rootjQuery);
}
})(jQuery);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜