JQuery custom pick

I would like to expand the jQuery selection so that it treats the given selection value as an identifier.

For example, when you execute $("foo.bar"), jQuery will try to select all the elements with the tag name foo and the class bar, but I want jQuery to select the element with the id foo.bar. Instead of deleting points and adding each time #, I would like to add as a function to jQuery so that I can do something like $("id:foo.bar").

Is this possible with jQuery? If so, can someone show me some examples?

Thank.

+3
source share
4 answers

, document.getElementById.

, . jQuery, :

jQuery.byId = function(id){return $(document.getElementById(id));};

$.byId('foo.bar').addClass('something');
+2

. , .expr[]. :

$.extend($.expr[':'], {
   'id': function(elem, i, attr){   
      return( elem.id === attr[3] );
   }
});

:

$(':id(foo.bar)');

: http://www.jsfiddle.net/cwwGk/1/

+3

$("[id=foo.bar]")

+2

. , jquery , . "sub" jquery 1.5. " jQuery", .

+1

Source: https://habr.com/ru/post/1790596/