使用JQuery,如何检查元素的前一个兄弟是否是第一个孩子?
我尝试了以下代码,但它不起作用:
if( $(this).prev() === $(this).siblings(":first-child") ){
//do something
}
Run Code Online (Sandbox Code Playgroud)
使用上述===方法的示例:http://jsfiddle.net/xnHfM/
if($(this).index() === 1){ // if index is 1 it means it's for sure the second children
// here u can grab $(this).prev(), and it's for sure the first one
}
Run Code Online (Sandbox Code Playgroud)
ps我会试着看看关于这个解决方案的性能vs.(':first-child')=> http://jsperf.com/jquery-check-previous-first-child
编辑:查看最后评论,谢谢@RightSaidFred :)