Jquery select class from variable

后端 未结 1 750
轻奢々
轻奢々 2021-02-13 20:04

I am using Jquery to find a class by variable. So,

var className = \"whatever\";

$(\"#container ul li\") if contains element with className

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-13 20:11

    Is the className on the

  • element? If so, you could do this:

    $('#container ul li.' + className)...
    

    You're just concatenating the className into the selector string (with the class selector).

    Or this will give you the same result.

    $('#container ul li').filter('.' + className)...
    

    Which is the similar to your .find() solution, but uses .filter() to limit the

  • elements found to those with the className.


    If the element with the className is a descendant of the

  • element, then using .find() should work, or you could do:

    $('#container ul li .' + className)...
    

    ...which almost looks the same as the one above, but introduces a space after li, which is a descendant selector.

0 讨论(0)
提交回复
热议问题