Set context so direct body children are never fetched with jQuery

I have the following HTML code:

<html>
  <head>
  </head>
  <body>
      <div id="never-selected">
      </div>
  </body>
</html>

      

As of JQuery 1.3.2, I would like to create a wrapper around $ () that will allow you to select any element with any combination, but never the selected div and its content.

I am trying to do it like this:

_context = false;    
getContext = function() {
    if (_context === false) {
        _gui_dom = jQuery("fuzzy magic selector");
     }
    return _gui_dom;
};


// return a jQuery object filtered to aim the content of the tartiflet GUI only
select = function(selector) {
  return jQuery(selector, getContext());  
};

      

But I want to find the correct formula to create a "fuzzy magic selector"; -)

My best shot jQuery("head *, body *:not(#never-selected, #never-selected *)")

, but the downside is that you can't choose a head or a body and that's very annoying. Using html * doest work.

0


a source to share


3 answers


I'm not sure if I figured out this problem, but try using . not () in the result set



select = function(selector){return $(selector).not('#never-selected, #never-selected *')};

      

+1


a source


You can write your own custom selector to parse the stuff you want using any amount of jQuery you need to capture those elements. Sometimes the selectors they provide by default may not do everything.

I cannot think of how to achieve what you are asking, but you can do something like this:

jQuery.extend(jQuery.expr[':'],{
    everything_but: function(el,sel) {
        var everything = jQuery(el).clone();
        everything.find(sel).remove();
        return everything;
    }
});

      

I definitely can't say my implementation will work, but it might give you an idea of ​​how you can implement your own selector to achieve what you want.



It can be used as usual. Something like that:

var select = $('*:everything_but(#never-selected)')

      

Writing your own selector is what you are trying to do with your scripts. Good luck!

0


a source


Is it possible to create a function that calls jQuery () with the arguments it passes and then automatically .filter () might work?

function select() {
  var jquery = jQuery.apply(args);
  return jquery.filter(":not(#never-selected, #never-selected *");
}

      

Not sure what works - it was coded in the air.

0


a source







All Articles