Selecting a variable's nested elements - jQuery

后端 未结 3 1264
攒了一身酷
攒了一身酷 2021-01-21 23:43

I wish to pass a variable to a function and have the function select elements within that variable. I\'m unfamiliar with the syntax of this case however, could any one advise?

相关标签:
3条回答
  • 2021-01-22 00:19

    For chainability you can also use:

    $(container).find('#element');
    
    0 讨论(0)
  • 2021-01-22 00:41

    jQuery gets a second parameter, which determines the scope of the selection. For example $('#first-name', '#registration-form') only matches elements with the first-name id inside registration-form.

    I guess you can use this feature. I think $("div#element", container); would work.

    See this fiddle.

    0 讨论(0)
  • 2021-01-22 00:43

    Use find if you are searching the Dom.

    function doSomething(container) {
      var $element = $(container).find('#element');
    
      // .. Now do something
    
    }
    

    Use filter if you are wanting to search only the elements contained by container.

    function doSomething(container) {
      var $element = $(container).filter('#element');
    
      // .. Now do something
    
    }
    
    0 讨论(0)
提交回复
热议问题