Getting first visible element with jQuery

前端 未结 3 427
我寻月下人不归
我寻月下人不归 2021-01-03 22:43

Trying to get the first visible element of a list using jQuery\'s :first and :visible pseudo-selectors, as suggested here: https://stackoverflow.co

相关标签:
3条回答
  • 2021-01-03 23:28

    Try using this:

    $('ul').find('li:visible:first').css('background','blue');
    

    Currently your code is just getting the first visible li element on the page and setting the background colour. This code selects all ul elements then finds the first visible li within each of them and applies the style.

    Here it is working: http://jsfiddle.net/FAY9q/5/

    0 讨论(0)
  • 2021-01-03 23:45

    What about using this:

    li:visible:not(:visible ~ :visible)
    
    0 讨论(0)
  • 2021-01-03 23:47
    $('li:visible').eq(0).css('background','blue');
    
    0 讨论(0)
提交回复
热议问题