Best practice syntactically for multiple jQuery selectors

If I were to bind a click event to a lot of different selectors using jQuery like the following:

scrollToPoint('.main-nav a, .sub-nav a, .enquire-btn, #showcase-list a, #showcase-more-info a.back-btn')

What is the industry standard method to lay this out syntactically and is their an alternative approach?


我认为这是最好的方法如果你确定你想要很多选择者:

var selectors = [
    ".main-nav a",
    ".sub-nav a",
    ".all",
    ".other",
    ".selectors"
];

scrollToPoint(selectors.join(', '));

function scrollToPoint(sel){
  $('#selectors').text(sel);  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="selectors"></div>

Selector optimization is less important than it used to be, as more browsers implement document.querySelectorAll() and the burden of selection shifts from jQuery to the browser. Came from jQuery's official site.

链接地址: http://www.djcxy.com/p/83460.html

上一篇: jQuery函数避免复制/粘贴

下一篇: 语法上适用于多个jQuery选择器的最佳实践