Multiple selector chaining in jQuery?

前端 未结 7 910
迷失自我
迷失自我 2020-11-29 03:23

Normally when I use a class as a selector I try to use an \"id\" selector with it so it does not search through the entire page but only an area where the class would be.

7条回答
  •  情话喂你
    2020-11-29 03:38

    You can combine multiple selectors with a comma:

    $('#Create .myClass,#Edit .myClass').plugin({options here});
    

    Or if you're going to have a bunch of them, you could add a class to all your form elements and then search within that class. This doesn't get you the supposed speed savings of restricting the search, but I honestly wouldn't worry too much about that if I were you. Browsers do a lot of fancy things to optimize common operations behind your back -- the simple class selector might be faster.

提交回复
热议问题