rle*_*mon 7 jquery jquery-selectors
给出以下内容
$("#identifier div:first, #idetifier2").fadeOut(300,function() {
// I need to reference just the '#identifier div:first' element
// however $(this) will grab both selectors
});
Run Code Online (Sandbox Code Playgroud)
除了再次调用$("#identifier div:first")之外,还有更好的方法吗?
不,它会分别调用每个句柄的功能.
选择器中的逗号相当于说:
$("#identifier div:first").fadeOut(300,function() {
// $(this) -> '#identifier div:first'
});
$("#idetifier2").fadeOut(300,function() {
// $(this) -> '#identifier2'
});
Run Code Online (Sandbox Code Playgroud)
您可以通过说(未经测试)来检查:
$("#identifier div:first, #idetifier2").fadeOut(300,function() {
if($(this).is("#identifier div:first") {
// do something
}
});
Run Code Online (Sandbox Code Playgroud)
但是,如果你想做不同的事情(就像你的文章中看到的那样),最好单独附加它们.