按照下面函数注释中的说明,我尝试选择“piece”类的每个成员,将其分成两半(使用 :even :odd 选择器),然后向它们添加两个不同的类。但我收到一条错误消息,说每个班级有 0 个成员。我选错了吗?
function setUpPieces() {
//select all the divs with class 'piece'
//add the 'light' class to half of them
//add the 'dark' to the other half
$('.piece:even').addClass('light');
$('.piece:odd').addClass('dark');
}
更新:
这些是说明
The jQuery selectors :even and :odd may be useful.
An example:
$('div:even')
selects half of the divs on the page: the first one (the one at index 0) and then every second div after it (the ones at indicies 2,4,6 ...)
$('div:odd')
selects the other half.