Can I combine a variable and a selector in Jquery?

前端 未结 3 2115
-上瘾入骨i
-上瘾入骨i 2021-02-15 14:12

Say I have this HTML:

top
middle
bottom
middle
相关标签:
3条回答
  • 2021-02-15 14:50

    You can use find()

    Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

    $top.find(".middle");
    
    0 讨论(0)
  • 2021-02-15 15:00

    Use

    $('.middle', $top)
    

    or

    $top.find('.middle')
    

    You could also have simply combined the selectors, which are strings, with

    $($top.selector + ' .middle')
    

    but this would only be slower and less readable...

    0 讨论(0)
  • 2021-02-15 15:06

    Could you just select it like this:

    $('.top .middle').click();
    
    0 讨论(0)
提交回复
热议问题