how to limit the jquery search scope

后端 未结 6 2302
别那么骄傲
别那么骄傲 2020-12-06 16:19

It looks like JQuery does the search in the current document when using a selector.

How to search for an element only inside a div

相关标签:
6条回答
  • 2020-12-06 16:42

    jQuery selectors work very much like CSS selectors, which you may be more familiar with.

    First, select the div, and then descend from that:

    $('#my-div').find('some-selector').
    

    or build your selector to match children of the element in question:

    $('#my-div some-selector')
    
    0 讨论(0)
  • 2020-12-06 16:46

    Old question, but everyone seems to have missed the scoped jQuery selector (using the scope you desired, i.e. your div selector, as the second parameter)

    e.g. use

    var $matches = $('.adiv', '#mydiv');
    

    This is a shorter equivalent of:

    var $matches = $('#mydiv').find('.adiv');
    
    0 讨论(0)
  • 2020-12-06 16:48

    jQuery provides several ways to search for specific elements:

    $("#your_div").find(".your_things");        //Find everything inside
      //-or-
    $("#your_div").filter(".your_things");      //Find only the top level
      //-or-
    $("#your_div .your_things");                //Easiest
    
    0 讨论(0)
  • 2020-12-06 16:49
    $('div-selector').find('the selector-you-are-looking-for');
    
    0 讨论(0)
  • 2020-12-06 17:00
    var elems = jQuery(".foo", jQuery("#divYourWantToLimitTo") );  //BAD
    //or
    var elems = jQuery("#divYourWantToLimitTo .foo");  //Better
    //or
    var elems = jQuery("#divYourWantToLimitTo").find(".foo");  //BEST
    
    0 讨论(0)
  • 2020-12-06 17:05
    var elements = $('div ' + yourSearch);
    
    0 讨论(0)
提交回复
热议问题