Can jQuery selectors be applied to an element rath

2019-04-22 21:12发布

问题:

jQuery('td[class=bgoff]').each(function() {
    var td = jQuery(this);
    ... no apply selector to "this" only
});

I'm working with tabular data in html and trying to parse the contents of each TD (they are not uniquely identifiable).

Using XPath, I can prepend the path of "this" to additional selecting.

How can I achieve this with jQuery?

回答1:

With jQuery you have the option of supplying a second parameter after the selector expression and that becomes a context that jQuery uses to limit scope of the lookup. Learn more here



回答2:

You can also use .find(expression) if you already have a jquery object within which you wish to search.

In your example:

jQuery('td[class=bgoff]').each(function() {
    var td = jQuery(this);
    $(td).find( <selector to search within td> );
});


回答3:

From the jQuery source:

// HANDLE: $(expr, context)  
// (which is just equivalent to: $(context).find(expr)